Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Firestore - Find `Document Snapshot` id

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?

like image 739
Kamil Harasimowicz Avatar asked Oct 08 '17 18:10

Kamil Harasimowicz


People also ask

How to get all data from a FireStore collection in flutter?

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!

How do I get data from Cloud Firestore?

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.

How do I filter documents within a collection in FireStore?

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.

How do I check if a documentsnapshot exists?

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:


3 Answers

Since the issue #12471 is resolved, you can get the document Id from the document object.

print(document.documentID)
like image 143
Purus Avatar answered Oct 18 '22 02:10

Purus


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

like image 2
Rene Avatar answered Oct 18 '22 03:10

Rene


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

like image 2
Liam Laird Avatar answered Oct 18 '22 01:10

Liam Laird