简体   繁体   中英

Get Data from StreamSubscription in Flutter from Firestore

I'm implementing a leaderboard in my Flutter App.

This is how I set up the stream:

Stream collectionStream = FirebaseFirestore.instance.collection('leaderboard').snapshots();
print(collectionStream.listen);

This is the result from my print:

I/flutter (17212): Closure: (((QuerySnapshot<Map<String, dynamic>>) => void)?, {Function? onError, (() => void)? onDone, bool? cancelOnError}) => StreamSubscription<QuerySnapshot<Map<String, dynamic>>> from Function 'listen':.

Can I access data from there now? I found the documentation here: https://firebase.flutter.dev/docs/firestore/usage/

But there is not listed how I can get my data with this method.

You can do:


FirebaseFirestore.instance.collection('leaderboard').snapshots()
.listen((QuerySnapshot collection) {
   // consume the collection being streamed / listened to
   for (var doc in collection.docs) {
      var docData = doc.data() as Map<String, dynamic>;
      // do what you want with your data here
   }

});

The snapshots call returns the Stream, now you need to listen to the stream.

The listen requires a callback on which you can capture the data being listened to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM