Flutter Firestore-查找“文档快照” ID [英] Flutter Firestore - Find `Document Snapshot` id

查看:88
本文介绍了Flutter Firestore-查找“文档快照” ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有产品列表的简单应用。产品存储在Firebase Firestore中。我想下载产品列表,并给用户提供更新某些数据的可能性。

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(),
      );
    },
  );
}

我将文档快照序列化为我的Product对象:

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];
    }
}

现在,当用户与ProductCell进行一些交互时在链接的Firestore中更新产品文档,但我没有ID,因此无法创建正确的Firestore参考。

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.

如何实现?

推荐答案

自#12471起解决后,您可以从文档对象中获取文档ID。

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

print(document.documentID)

这篇关于Flutter Firestore-查找“文档快照” ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆