簡體   English   中英

如何從不同的 dart 文件訪問變量?

[英]How to access variables from different dart files?

我已將文檔 ID 存儲在字符串doc_id中。 我想使用此 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));
           }

我嘗試了另一種方法,比如將此 doc_id 作為字段值存儲在集合“博客”中,

 await FirebaseFirestore.instance
                           .collection('blogs')
                          .doc(docid)
                          .update({'postid': docid});

但是在使用 get() 獲取數據時,它需要用於獲取的documentid 有什么方法可以在沒有 documentid 的情況下讀取值。 或者如何從另一個 dart 文件訪問此 doc_id?

您可以閱讀集合以獲取所有博客,或者使用它運行查詢以防止一次讀取過多文檔而增加成本,例如FirebaseFirestore.instance.collection('blogs').limit(10).snapshots()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM