as I'm further progressing with my code and my coding experiences in Flutter. I've encountered different types of snapshots, while working with the Firebase API. I'm talking about AsyncSnapshots, QuerySnapshots, DocumentSnapshots and DataSnapshots. If there are more pls name them too.
I was wondering, what the exact differences are between those snapshots.
What I've figured so far is that an AsyncSnapshot is probably a Snapshot that is taken asynchronously, meaning the Widget is built before the Data of the Snapshot is available therefore making it async (pls correct me if I'm wrong). Here is where my confussion starts, what exactly is a Snapshot? And what is the "Data" in each of them. For example: Why can't the same function retrieve the needed Data in all of the Snapshots, but is only functioning on a particular Snapshot.
Why is it necessary to convert the Data from a QuerySnapshot to a DocumentSnapshot, to make it accesible (Again pls correct me if I'm wrong)? And what is the exact difference between DocumentSnapshot and DataSnapshot. And why are they called differently, when both of them return Maps?.
Thank you in advance.
As far as I can tell you are asking this in the context of Flutter, so I'll answer for that below.
There are two databases in Firebase: the original Realtime Database, and the newer Cloud Firestore. Both are equally valid options today, but they are completely separate with their own API. But both return snapshots of data, where the snapshot is a copy of the data from the database in your application code.
In Flutter you have FutureBuilder
and StreamBuilder
, which deal with snapshots of data that is loaded asynchronously.
Let's see if I can cover them:
DocumentSnapshot
with its data. And if you load a list of document, you get a QuerySnapshot
that you then loop over to access the individual DocumentSnapshot
s.So in Flutter you'll have an AsyncSnapshot
that refers to one of the Firebase snapshot classes, and that Firebase snapshot then wraps the actual data.
Say you want to display a list with the documents in a collection from Firestore, you'll have:
AsyncSnapshot
to feed to your StreamBuilder
, so that it can render the correct state of data loading.QuerySnapshot
for the list of documents from the database.DocumentSnapshot
with a snapshot of the data from a single document.I actually find this much easier to see in code, as in this example from the FlutterFire documentation:
class UserInformation extends StatelessWidget {
@override
Widget build(BuildContext context) {
CollectionReference users = FirebaseFirestore.instance.collection('users');
return StreamBuilder<QuerySnapshot>(
stream: users.snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Text('Something went wrong');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Text("Loading");
}
return new ListView(
children: snapshot.data.documents.map((DocumentSnapshot document) {
return new ListTile(
title: new Text(document.data()['full_name']),
subtitle: new Text(document.data()['company']),
);
}).toList(),
);
},
);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With