Flutter Firestore - Find `Document Snapshot` id

2019-05-21 03:13发布

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?

2条回答
Evening l夕情丶
2楼-- · 2019-05-21 03:34

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

print(document.documentID)
查看更多
你好瞎i
3楼-- · 2019-05-21 03:34

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

查看更多
登录 后发表回答