在Dart的Cloud Firestore中添加文档后如何获取文档ID

再次学习

我使用以下代码通过Dart / Flutter更新了Cloud Firestore集合。

  final docRef = Firestore.instance.collection('gameLevels');
  docRef.document().setData(map).then((doc) {
    print('hop');
  }).catchError((error) {
    print(error);
  });

我正在尝试将文档添加到集合中时创建的documentID,但(doc)参数返回为null。我以为应该是documentReference?

由于为空,因此我显然不能使用doc.documentID。

我究竟做错了什么?

加纳帕特

您可以尝试以下操作:

DocumentReference docRef = await 
Firestore.instance.collection('gameLevels').add(map);
print(docRef.documentID);

由于add()方法创建新文档并自动生成ID,因此您不必显式调用document()方法

或者,如果您想使用“ then”回调,请尝试以下操作:

  final collRef = Firestore.instance.collection('gameLevels');
  DocumentReferance docReference = collRef.document();

  docReferance.setData(map).then((doc) {
    print('hop ${docReferance.documentID}');
  }).catchError((error) {
    print(error);
  });

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

新更新后,如何在 Dart 的 Cloud Firestore 中添加文档后获取文档 ID

在Dart Flutter中将文档添加到Firestore后如何直接获取文档ID

如何获取集合Cloud Firestore中的文档ID列表?

在 Cloud Firestore 中,如何添加具有显式文档 ID 的文档

如何在 Flutter 上获取 Cloud Firestore 上的文档 ID

在Firebase Cloud Functions(Firestore)中获取文档ID

如何从Cloud Firestore中获取user.uid等于flutter中的文档ID的数据?

如何在Firebase / Google Cloud Firestore的集合中获取最新添加的文档?

如何获取给定数组中不存在的ID的所有文档?(Cloud Firestore)

Cloud Firestore查询以获取文档ID,Flutter

SwiftUI:如何迭代 Cloud Firestore 集合中的文档并获取数据?

如何在 Cloud Firestore Swift 中获取文档的 UID

如何根据索引从 Cloud Firestore 集合中获取文档?

如何使用 Cloud Firestore 中的文档 ID 执行“where”查询

Cloud Firestore 文档 ID

在Flutter中获取Cloud Firestore文档的路径

在Firestore Cloud功能中获取文档

如何在Cloud Firestore中有效地获取不同集合中具有特定ID的文档?

如何使用模块化 Web SDK 的版本 9 在 Cloud Firestore 集合中的文档中获取文档和嵌套集合?

如何获取具有id的特定文档数据?| AngularFire 5.1.1 | Cloud Firestore | 文件资料

如何获取特定的文档ID?AngularFire 5.1.1 | Cloud Firestore | 文件资料

如何在flutter中使用cloud firestore发布期间获取当前自动生成的文档ID?

是否可以获取 Firebase Cloud Firestore 数据库中可用集合的文档 ID?

在Cloud Firestore中获取文档的一个特定的自动生成的ID

如何使用Android在Cloud Firestore中查询特定文档后的集合?

Flutter - 如何仅获取 Cloud Firestore 中列表中不包含的特定值的文档?

如何从 Cloud Functions 添加带有 AutoID 的 Firestore 文档?

在Cloud Firestore中删除文档

为什么未在Cloud Firestore中按顺序添加文档?