简体   繁体   中英

The argument type 'Stream<dynamic>?' can't be assigned to the parameter type 'Stream<QuerySnapshot>?'

I am trying to use StreamBuilder to show a stream of text messages from an firestore database. But for some reason the argument type of Stream can't be assigned to the parameter of Querysnapshot . If I get rid of the Querysnapshot the stream builder doesn't show the error anymore but then I get an error:

The getter 'documents' isn't defined for the type 'Object'

Can anyone help me out with this problem?

Widget ChatMessageList() {
return StreamBuilder(
  stream: chatMessagesStream,
  builder: (context, snapshot) {
    return ListView.builder(
      itemCount: snapshot.data!.documents.length,
      itemBuilder: (context, index) {
        return MessageTile(snapshot.data!.documents[index].data['message']);
      },
    );
  },
);

}

This kind of message appears because dart does not know what kind of object snapshot.data is. You probably need to specify the expected type that will be returned from your stream. What does chatMessagesStream return? If it is Stream<Bar> , then write (context, AsyncSnapshot<Bar> snapshot) , like in the official example StreamBuilder

Also, you should not do calls directly to firebase from your widgets. That is going to create lots of copy-paste and messy code. You can find an example of a cleaner architecture here: real-time-updates-with-streambuilder

It will be a little extra work to set it up, but it will definitely pay off quickly ass you include more calls in your code.

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