Waseem Ahmed
Waseem Ahmed

Reputation: 419

Firestore query is returning data but Futurebuilder saying it cannot be null

I am trying to build a list from documents i have in Firestore. below is my Future function:

Future getBuyerList() async {

await Firestore.instance.collection('Deals').where('buyer_id',isEqualTo: uid_global).snapshots().listen((data) {
  data.documents.forEach((doc) {

    print('in function -  ${doc["car"]}');


    print(doc.data);


  });
});


}

The above runs fine in the debugger. it shows the full list of items i am expecting to put into the List.

My Future Builder in my widget tree is as follows:

FutureBuilder(

                future: getBuyerList(),

                builder: (BuildContext context, snapshot) {


                if(snapshot.hasData){
                  if(snapshot.connectionState == ConnectionState.waiting){

                    return Center(

                      child: CircularProgressIndicator(),

                    );


                  }else{

                    Center(

                      child: Text('data loaded')

                    );

                  }

                }else if (snapshot.hasError){

                  Text('no data');

                }

                },
              ),

I have not yet loaded the snapshot into the Listview as i am trying to get the FutureBuilder to atleast run successfully. but i keep getting a FutureBuilder null error.

Null error on Future Builder.

I tried running the function as a non void function as well by returning a list but then i get an error on compile saying i cannot assign a Map to Future>.

Please help.

@Peter Haddad's answer was correct but i also needed to make a change to the builder by adding AsyncSnapshot<QuerySnapshot>:

builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot)

Upvotes: 1

Views: 624

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80904

You need to add return when using the FutureBuilder:

           FutureBuilder(
                future: getBuyerList(),
                builder: (BuildContext context, snapshot) {
                if(snapshot.hasData){
                  if(snapshot.connectionState == ConnectionState.waiting){
                    return Center(
                      child: CircularProgressIndicator(),
                    );
                  }else{
                   Center(
                      child: Text('data loaded')
                    );
                  }
                }else if (snapshot.hasError){
                  Text('no data');
                }
                  return CircularProgressIndicator();
                },
              ),

return CircularProgressIndicator(); since the AsyncWidgetBuilder returns a Widget. Also if the FutureBuilder is the top widget in the build function then you need to return it:

return FutureBuilder( /*...*/)

Regarding the function getBuyerList() do the following:

  Future<QuerySnapshot> getBuyerList() async {
    return await Firestore.instance
        .collection('Deals')
        .where('buyer_id', isEqualTo: uid_global)
        .getDocuments();
  }

Upvotes: 3

Related Questions