Home > Software design >  NoSuchMethodError: The getter 'docs' was called on null. Flutter
NoSuchMethodError: The getter 'docs' was called on null. Flutter

Time:03-30

I am trying to develop a flutter app. This is a simple chat app with some webview. After create all backend and in firebase news as well as new users show up. in the app, messages won't show up in chat. i have that error when i try to go to chat room.

NoSuchMethodError (NoSuchMethodError: The getter 'docs' was called on null.

Receiver: null

Tried calling: docs)

this is my code

class _ConversationScreenState extends State<ConversationScreen> {
  DatabaseMethods databaseMethods = new DatabaseMethods();
  TextEditingController messageController = new TextEditingController();

  Stream chatMessageStream;

  Widget ChatMessageList() {
    return StreamBuilder(
      stream: chatMessageStream,
      builder: (
        context,
        snapshot,
      ) {
        return ListView.builder(
            itemCount: snapshot.data.docs.lenght, //ERROR
            itemBuilder: (context, index) {
              return MessageTile(snapshot.data.docs[index].data["message"]); //ERROR
            });
      },
    );
  }

CodePudding user response:

There is a chance that the snapshot being returned might be null if the stream hasn't yet emitted something. Use an if checker to check if the snapshot is null.

if(snapshot != null && snapshot.hasData){

    return ListView.builder(
        itemCount: snapshot.data.docs.lenght, //ERROR
        itemBuilder: (context, index) {
          return MessageTile(snapshot.data.docs[index].data["message"]); //ERROR
        });

}else {
   
  return Container();

}
  • Related