Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I get the last uploaded data to Firebase?

StreamBuilder<QuerySnapshot<Map<String, dynamic>>>(
        stream: FirebaseFirestore.instance
            .collection('users')
            .where('bmi')
            .limit(1)
            .snapshots(),
        },
      )),


class User {
  final double bmi;

  User(
      {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>>>?’.",

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Gives a fault.

>Solution :

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:

FirebaseFirestore.instance
    .collection('users')
    .orderBy('yourTimestampField', descending: true)
    .limit(1)
    .snapshots(),
},

Also see the Firebase documentation on ordering and limiting data

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading