首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Firestore offline persistence flutter docId

Firestore offline persistence flutter docId
EN

Stack Overflow用户
提问于 2021-01-21 21:22:42
回答 1查看 63关注 0票数 0

我有一个使用firestore的应用程序,当用户单击一个按钮时,我添加了一个文档,在文档创建后,我还在字段中添加了它的documentId,这是由firestore生成的字母数字字符串。我的问题是,如果用户在没有互联网的情况下点击按钮,然后关闭应用程序并通过互联网连接打开它,那么文档就会被创建,但它里面的Id当然是空的,因为程序员停止了执行。有没有办法让我坚持下去呢?

例如

代码语言:javascript
复制
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});

更新版本没有离线持久化,有什么办法可以解决吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-21 22:44:15

正如Dima所评论的那样,您可以通过首先生成ID来解决这个问题,然后才能使用数据及其ID编写文档。

要在不写入的情况下生成文档ID,可以使用call doc() on the CollectionReference without an argument

代码语言:javascript
复制
DocumentReference documentReference =
     FirebaseFirestore.instance.collection('sth').doc();

map["docId"] = documentReference.id;

FirebaseFirestore.instance
  .collection('sth')
  .doc('${documentReference.id}')
  .set(map);

现在,第一行是一个纯客户端操作,还没有任何数据写入数据库-因此您最终得到了单个原子写/set()

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65828364

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档