How can I get the last uploaded data to Firebase?

How can I get the last uploaded data to Firebase?

Problem Description:

StreamBuilder<QuerySnapshot<Map<String, dynamic>>>(
        stream: FirebaseFirestore.instance

class User {
  final double bmi;

      {required this.bmi});

  Map<String, dynamic> toJson() => {
        'bmi': bmi,

  static User fromJson(Map<String, dynamic> json) => User(
      bmi: json['bmi']);

I want to pull the last added ‘bmi’ value to firebase. Do I need to use the OrderBy() method?

I tried adding the .last method to the end of the snapshot. But

"The argument type ‘Future<QuerySnapshot<Map<String, dynamic>>>’ can’t be assigned to the parameter type ‘Stream<QuerySnapshot<Map<String, dynamic>>>?’.",

Gives a fault.

Solution – 1

Firestore has no built in concept of a timestamp for each document of when it was created or updated that you can use as a condition in a query.

If you need this, you’ll have to add a timestamp field to each document yourself. Once that exists, you can get the latest document with:

    .orderBy('yourTimestampField', descending: true)

Also see the Firebase documentation on ordering and limiting data

Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.