简体   繁体   English

从 Firestore 的 Flutter 中的 StreamSubscription 获取数据

[英]Get Data from StreamSubscription in Flutter from Firestore

I'm implementing a leaderboard in my Flutter App.我正在我的 Flutter 应用程序中实现排行榜。

This is how I set up the stream:这就是我设置 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/我在这里找到了文档: 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.快照调用返回 Stream,现在您需要收听 stream。

The listen requires a callback on which you can capture the data being listened to.监听需要一个回调,您可以在回调上捕获正在监听的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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