Scenario explained: Using: import 'package:cloud_firestore/cloud_firestore.dart';
Cloud Firestore: a Collection 'sessionsLog', containing documents with the following fields: 'sessionId' a String 'isClosed' a bool 'locationId' a String and some other fields ...
I want to retrieve all documents with a false bool for 'isClosed', only once, and then perform some actions. Therefore I chose to use a FutureBuilder (not a streambuilder), what I did:
FutureBuilder(
future: Firestore.instance.collection('sessionsLog')
.where('isClosed', isEqualTo: false).getDocuments();
builder: (ctx, sessionSnapshot) {
return RaisedButton(
child: Text('Search'),
onPressed: () async {
//some other code here, that needs async I added on top
if (sessionSnapshot.connectionState !=
ConnectionState.done) {
return CircularProgressIndicator();
}
if (!sessionSnapshot.hasData) {i should
print('nothing to show here');
}else {
//the below line of code is wrong, it could be print / or Text / or any other widget
//I've just added it for the sake of an example.
print(sessionSnapshot.data.documents[0]['parkingLocationName']);}});}),
The above code is not working, I've just created it for a simple code result, to just be able to print values I am retrieving, so please don't fix something in my code.
Also noting that, in my code logic, there will always be once single document with 'isClosed' = false
. So, I should always receive one single document.
But If it differs, please give me 2 examples, one if retrieved data contains 1 single document and another if there could be several documents and therefore how would I iterate in the data.
I just want the correct way to be able to READ/access the data I am receiving, whether to print the fields info, or display them in a text. anything, but how would I do that, in the exact explained above scenarios.
Would really appreciate a simple sample example based on my code scenario.
limit
andorderBy
in the Firestore documentation. – Tom Bailey