Reputation: 21
I am new to Firebase and try to use the Firestore.
My goal is, to simply put some data in and out of the database.
My database looks like:
Collection: user
Document: userID
Content:
forename: Steve
surename: Bobbins
If i'll try to acces the "forename", how would i do this on the most efficient way? I am somewhat confused with the snapshot stuff and here's how i'm going so far:
DocumentSnapshot doc = await FirebaseFirestore.instance.collection("user").doc("21938103").snapshots().first;
print(doc["surname"]);
I know there is only one document with this id, but i am not sure if there are better ways to handle this.
Upvotes: 1
Views: 327
Reputation: 12353
FirebaseFirestore.instance.collection("user").doc("21938103").snapshots().listen(
(event){
String forename = event.get('forename');
print(forename);
});
If you want a specific field like forename
, you use .get('forename')
, or the name of field you want. If you want the entire document, you use:
Map<String, dynamic> _map = event.data() as Map<String, dynamic>;
print(_map['forename']);
print(_map['surename']);
Upvotes: 2