I hava simple app with list of products. Products are stored on Firebase Firestore. I want to download list of products and give user possibility to update some data.
So I prepare list with products:
Widget _buildProductsList() {
return new StreamBuilder(
stream: Firestore.instance
.collection('products')
.snapshots,
builder: (context, snapshot) {
if (!snapshot.hasData) return new Text("Loading...");
return new ListView(
children: snapshot.data.documents.map((document) {
Product product = new Product(document.data);
_ProductCell cell = new _ProductCell();
cell.product = product;
return cell;
}).toList(),
);
},
);
}
I serialize Document Snapshot to my Product object:
class Product {
static const NAME_KEY = 'name';
static const DESC_KEY = 'desc';
static const PHOTO_URL_KEY = 'photoUrl';
static const AUTHOR_ID_KEY = 'authorId';
static const CREATED_AT_KEY = 'createdAt';
String name;
String description;
String photoUrl;
String authorId;
int createdAt;
Product(Map<String, dynamic> json) {
name = json[NAME_KEY];
description = json[DESC_KEY];
photoUrl = json[PHOTO_URL_KEY];
authorId = json[AUTHOR_ID_KEY];
createdAt = json[createdAt];
}
}
Now, when user make some interact with ProductCell I want to update Product Document in Firestore which is linked with but I don't have an ID so it is impossible to create proper Firestore reference.
How to achieve this?
So today, in this article, we will learn about How to get all data from a Firestore collection in flutter. How to get all data from a Firestore collection in flutter? Call the getDocs () function, then use the build function, then print all the document IDs in the console. Here is how you do it!
Get data with Cloud Firestore. There are two ways to retrieve data stored in Cloud Firestore. Either of these methods can be used with documents, collections of documents, or the results of queries: Call a method to get the data. Set a listener to receive data-change events.
Cloud Firestore offers advanced capabilities for querying collections. Queries work with both one-time reads or subscribing to changes. To filter documents within a collection, the where method can be chained onto a collection reference. Filtering supports equality checks and "in" queries.
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. To determine whether the document exists, use the exists property:
Since the issue #12471 is resolved, you can get the document Id from the document object.
print(document.documentID)
I have made an issue about this yesterday as I encountered the same bug. You can track it here: https://github.com/flutter/flutter/issues/12471
Currently the FireStore plugin has some more issues. You can see all issues with Firebase plugins by filtering on the plugin: https://github.com/flutter/flutter/labels/plugin%3A%20firebase
the syntax for this has since been updated you can now get it from
print(document.id);
https://pub.dev/documentation/firebase/latest/firebase_firestore/DocumentSnapshot/id.html
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