我有一个使用firestore的应用程序,当用户单击一个按钮时,我添加了一个文档,在文档创建后,我还在字段中添加了它的documentId,这是由firestore生成的字母数字字符串。我的问题是,如果用户在没有互联网的情况下点击按钮,然后关闭应用程序并通过互联网连接打开它,那么文档就会被创建,但它里面的Id当然是空的,因为程序员停止了执行。有没有办法让我坚持下去呢?
例如
DocumentReference documentReference =
await FirebaseFirestore.instance.collection('sth').add(map); //map has a 'docId' key
await FirebaseFirestore.instance
.collection('sth')
.doc('${documentReference.id}')
.update({'docId': documentReference.id});更新版本没有离线持久化,有什么办法可以解决吗?
发布于 2021-01-21 22:44:15
正如Dima所评论的那样,您可以通过首先生成ID来解决这个问题,然后才能使用数据及其ID编写文档。
要在不写入的情况下生成文档ID,可以使用call doc() on the CollectionReference without an argument。
DocumentReference documentReference =
FirebaseFirestore.instance.collection('sth').doc();
map["docId"] = documentReference.id;
FirebaseFirestore.instance
.collection('sth')
.doc('${documentReference.id}')
.set(map);现在,第一行是一个纯客户端操作,还没有任何数据写入数据库-因此您最终得到了单个原子写/set()。
https://stackoverflow.com/questions/65828364
复制相似问题