我正在使用以下代码使用DART/Ffltter更新Cloud Firestore集合.

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

我试图获取在将文档添加到集合时创建的DocumentID,但(Doc)参数返回为空.我以为这应该是一个文件引用?

因为它是空的,所以我显然不能使用doc.document ID.

我做错了什么?

推荐答案

您可以try 以下方法:

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

因为add()方法创建新文档并自动生成id,所以您不必显式调用document()方法

或者,如果您想使用"THEN"回调,请try 以下操作:

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

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

Dart相关问答推荐

Dartpad使用基本身份验证发出请求

Dart 列表中的 addAll() 和 followBy() 有什么区别?

根据 Id 比较两个不同的 List

如何使用 http 库捕获 SocketException?

如何在 dart 中获取当前和调用函数的名称?

Flutter - 可以在没有 Firebase 的情况下使用 Google 登录吗?

如何在Flatter中将图表线 colored颜色 更改为自定义 colored颜色 代码值

如何go 除字符串中的变音符号?

如何在Flatter中的屏幕中心创建选项卡栏?

如何创建带有固定页脚和Flutter的滚动视图?

如何更改手机屏幕上的Flutter应用程序名称显示?

如何在dart中四舍五入?

使用 dart 下载文件

如何判断switch/case中对象的类型?

Dart:打印整数和字符串

在 Dart 中,List.from 和 .of 以及 Map.from 和 .of 有什么区别?

Dart null / false / empty checking:如何写这个更短?

Dart 会支持使用现有的 JavaScript 库吗?

了解工厂构造函数代码示例

Dart 如何获取枚举的value