简体   繁体   English

如何从不同的 dart 文件访问变量?

[英]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.

相关问题 如何从另一个 dart 文件访问值? - How to access values from another dart file? 如何使 Dart 在全球范围内检测来自不同文件的枚举扩展名? - How to make Dart detects enum extensions from different file globally? 如何使用 Dart 访问数据库中的值? - How to access a value in the database using Dart? 如何在服务器端访问 dart 中的 firestore - How to access firestore in dart on server side 如何从 Powershell 脚本文件访问 Azure DevOps 预定义变量 - How to access Azure DevOps Predefined variables from Powershell script file 从 Flutter/Dart 访问受 AWS S3 保护的图像? - Access AWS S3 protected Images from Flutter/Dart? 如何使用 java 从 .properties 访问环境变量? - How to access environment variables from .properties using java? 如何从 AzureML 文件共享访问本地文件? - How to access local files from AzureML File Share? 如何从不同的 AWS 区域访问 RDS Proxy? - How to Access RDS Proxy from a Different AWS Region? 如何从 firebase 获取数据并将其存储到列表中以供在 dart 中进一步操作 - How to get data from the firebase and store it into a list for futher manipulation in dart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM