我使用以下代码通过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] 删除。
我来说两句