简体   繁体   中英

How to access variables from different dart files?

I have stored my document ID inside a string doc_id . I want to use this doc_id to access the subcollection from another file. This is how I stored the 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',

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

But then while fetching the data using get() it needed the documentid for fetching. Is there any way that I can read values without documentid. Or how can I access this doc_id from another dart file?

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM