1

The code Works with firebase and however, when I do that, an error saying:

The following NoSuchMethodError was thrown building StreamBuilder(dirty, state: _StreamBuilderBaseState<dynamic, AsyncSnapshot>#458db): The getter 'documents' was called on null. Receiver: null Tried calling: documents

 Widget _listBuilder() {
   if (cars != null) {
     return StreamBuilder (
       stream: cars,
       builder: (context, snapshot) {
         return ListView.builder(
           // ignore: deprecated_member_use
           itemCount: snapshot.data.documents.length,
           itemBuilder: (context, i) {
             return ListTile(
               // ignore: deprecated_member_use
               title: Text(snapshot.data.documents[i].data()['carName']),
               // ignore: deprecated_member_use
               subtitle: Text(snapshot.data.documents[i].data()['carColor']),
               // ignore: deprecated_member_use
               trailing: Text(snapshot.data.documents[i].data()['manYear']),
               onTap: () {
                 updateDialog(context, snapshot.data.documents[i].documentID);
               },
               onLongPress: () {
                 crudObj.deleteData(snapshot.data.documents[i].documentID);
               },
             );
           },
         );
       },
     );
   }
   else {
     return Text("Please Wait");
   }
 }
Owczar
  • 2,335
  • 1
  • 17
  • 24
Haree Prasad
  • 91
  • 1
  • 4
  • 1
    Does this answer your question? [What is a NoSuchMethod error and how do I fix it?](https://stackoverflow.com/questions/64049102/what-is-a-nosuchmethod-error-and-how-do-i-fix-it) – nvoigt Nov 24 '20 at 13:12

2 Answers2

0

You should check if snapshot.hasData before other checks. Also you should be aware of snapshot.hasError and snapshot.connectionState. See StreamBuilder example in order to build async widget.

Owczar
  • 2,335
  • 1
  • 17
  • 24
0

Try to check if your snapshot has data or not before directly using data

Here is complete code

 if (cars != null) {
     return StreamBuilder (
       stream: cars,
       builder: (context, snapshot) {
         if(snapshot.hasData){
           return ListView.builder(
           // ignore: deprecated_member_use
           itemCount: snapshot.data.documents.length,
           itemBuilder: (context, i) {
             return ListTile(
               // ignore: deprecated_member_use
               title: Text(snapshot.data.documents[i].data()['carName']),
               // ignore: deprecated_member_use
               subtitle: Text(snapshot.data.documents[i].data()['carColor']),
               // ignore: deprecated_member_use
               trailing: Text(snapshot.data.documents[i].data()['manYear']),
               onTap: () {
                 updateDialog(context, snapshot.data.documents[i].documentID);
               },
               onLongPress: () {
                 crudObj.deleteData(snapshot.data.documents[i].documentID);
               },
             );
           },
         );
         }
       },
     );
   }
   else {
     return Text("Please Wait");
   }
Saiful Islam
  • 1,151
  • 13
  • 22