[英]How to access variables from different dart files?
I have stored my document ID inside a string doc_id
.我已将文档 ID 存储在字符串
doc_id
中。 I want to use this doc_id to access the subcollection from another file.我想使用此 doc_id 从另一个文件访问子集合。 This is how I stored the doc_id:
这就是我存储 doc_id 的方式:
onPressed: () async {
await FirebaseFirestore.instance
.collection('blogs')
.add({
'postbody': postController.text,
}).then((value) {
doc_id = value.id;
}).catchError((error) => errorAlert(context));
}
I have tried another way, like storing this doc_id as a field value inside the collection 'blog',我尝试了另一种方法,比如将此 doc_id 作为字段值存储在集合“博客”中,
await FirebaseFirestore.instance
.collection('blogs')
.doc(docid)
.update({'postid': docid});
But then while fetching the data using get() it needed the documentid
for fetching.但是在使用 get() 获取数据时,它需要用于获取的
documentid
。 Is there any way that I can read values without documentid.有什么方法可以在没有 documentid 的情况下读取值。 Or how can I access this doc_id from another dart file?
或者如何从另一个 dart 文件访问此 doc_id?
You can read on collection to get all blog, Or run query with it to prevent read too much document one time that can increased costs, like FirebaseFirestore.instance.collection('blogs').limit(10).snapshots()
您可以阅读集合以获取所有博客,或者使用它运行查询以防止一次读取过多文档而增加成本,例如
FirebaseFirestore.instance.collection('blogs').limit(10).snapshots()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.