简体   繁体   中英

snapshot never shows null even if there is no data in it

I am facing the error that the snapshot inside the stream builder never shows null , even though there is no data in it.

I have printed the values of the snapshot.data / snapshot.hasData but once I run it, it shows null/false but just within minutes it starts showing not null / true

Maybe the error is because of the using Stream Builder or something else Can you please help if you got the solution or what alternative you have used for this:

The full code is here: Accounts.Dart

StreamBuilder(
        stream: Accountforuser(),
        builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
          print('Here is test data ' +
// snapshot.hasData.toString());
// snapshot.hasData);
// snapshot.isEmpty);
 snapshot.data=null);

          if(snapshot.stackTrace.toString()==null){
           isLoading=false;
           return Text('No Data');
          }
else{
< Here is Data>
}

I was curious about using this

AsyncSnapshot<QuerySnapshot> snapshot)

But don't know.

I have printed the values of snapshot.data / snapshot.hasData but once I run, it shows null/false but just within minutes it starts showing not null/true.

I am expecting the solution or an alternative to using Streambuilder or the QuerySnapshot

from the official documentation :

A DocumentSnapshot is returned from a query, or by accessing the document directly. Even if no document exists in the database, a snapshot will always be returned.

so the snapshot will never be null even if no document is there, but you can check over them with the exists like this:

    if (snapshot.hasData && snapshot.data!.exists) {
      return Text("Document does exist");
    } else {
      return Text("Document does not exist");
    }

so in your case, you can replace:

     if(snapshot.stackTrace.toString()==null){
       isLoading=false;
       return Text('No Data');
      }

with:

      if(!snapshot.data!.exists){
       isLoading=false;
       return Text('No Data');
      }

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