'Is a Firestore Query Snaphot in flutter ever null?
In my flutter app I am checking if a document exists based on certain parameters and based on snapshot.hasData build the widget.
However I remember reading somewhere, at least when coding in Kotlin (and I assume should be true with Flutter), that a snapshot can never be null??
If this is the case, how can I check if a document exists or not and use the fact that the document does not exist to build the widget. i know i am using a query snapshot
i have it running as follows:
FutureBuilder(
future: checkSeller(deal_id_chosen),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot2) {
if (snapshot2.connectionState ==
ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(),
);
} else if (snapshot2.connectionState == ConnectionState.done) {
if(snapshot2.data['seller_id'] == null){
return Text('doesnt exist');
}
if(snapshot2.data['seller_id'] == uid_global){
return Text('seller exists');
}
} else if (snapshot2.hasError) {
return Text('no data');
}
return CircularProgressIndicator();
},
),
My Firebase Query a follows (this runs fine but always returns as true, even when a document should not exist)
Future<QuerySnapshot> checkSeller(deal_id) async{
return await Firestore.instance.collection('Deals').where('deal_id'.isEqualTo:deal_id).where('seller_id',isEqualTo:uid_global).getDocuments();
}
Can you do such a query, because I was expecting the FutureBuilder to not satisfy the snapshot2.hasData and hence allow me to switch between either building a widget saying that the seller exists or seller doesn't exist in my case.
Solution 1:[1]
Not having any results is not an error condition for a query. To check if a query had any results, check if the length
of the QuerySnapshot.documents
property is greater than 0.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Frank van Puffelen |