Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closure: () => Map<String, dynamic> from Function 'data':

So im trying to stream data from firestore but when printing the data I get:

I/flutter ( 8356): Closure: () => Map<String, dynamic> from Function 'data':.

I am using this code to fetch the data:

 void messagesStream() async {
    Stream collectionStream = _firestore.collection('messages').snapshots();
    await for (var snapshot in collectionStream) {
      for (var message in snapshot.docs) {
        print(message.data());
      }
    }

When new data is added to the messages collection I get the Closure message so it is interacting with the databse.

What I want is it to print out the contents of the new document within the collection.

Any help is appreciated.

like image 598
Daniel Booth Avatar asked Oct 27 '25 03:10

Daniel Booth


1 Answers

That's not the way you're supposed to iterate the results of a Stream. If you have a Stream and you want to process its results, you're supposed to use listen() to receive the results asynchronously.

Stream collectionStream = _firestore.collection('messages').snapshots();
collectionStream.listen((QuerySnapshot querySnapshot) {
    querySnapshot.documents.forEach((document) => print(document.data()));
}

See also: Firestore collection query as stream in flutter

You might also want to review the documentation to learn how to query Firestore in Flutter.

like image 156
Doug Stevenson Avatar answered Oct 30 '25 00:10

Doug Stevenson