简体   繁体   English

如何使用 flutter 在 Firestore 中特定文档的字段内检索 Map 值?

[英]How to retrieve a Map values inside a field of a specific document in firestore using flutter?

I want to get all the Values of the map which is under "Subjects" field.我想获取“主题”字段下的 map 的所有值。 And I also want to listen if any updates or changes made to those values.我也想听听是否对这些值进行了任何更新或更改。 I actually want to add al those values and store in a variable.我实际上想添加所有这些值并存储在一个变量中。 Can someone please tell me how can i achieve this?有人可以告诉我如何实现这一目标吗?

Here is the structure of the collection.这是集合的结构。 在此处输入图像描述

This is how im adding the subjects whenever a user enters the subject name这就是每当用户输入主题名称时我添加主题的方式

onPressed: () async {
                      temp = qrdataFeed.text;
                        int index = qrdataFeed.text.indexOf('-');
                        subject = qrdataFeed.text.substring(0, index);
                        print("Subject name is $subject");
                        numberOfClasses = await FirebaseFirestore.instance
                            .collection('tutors')
                            .doc(uid)
                            .get()
                            .then((doc) async {
                          Map<String, dynamic> map = await doc.data();
                          if (!map.containsKey('Subjects')) {
                            await tutor_details.doc(uid).set({
                              'Subjects': {'$subject': initialValue},
                            }, SetOptions(merge: true));
                          }
                          if (doc.data()['Subjects']['$subject'] !=
                              '$subject') {
                            if (!map.containsKey('$subject')) {
                              await tutor_details.doc(uid).set({
                                'Subjects': {'$subject': initialValue}
                              }, SetOptions(merge: true));
                            }
                          }
                          var val = await doc.data()['Subjects']['$subject'];
                          return val;
                        });

                        if (!mounted) return;
                        setState(() {
                          qrData = qrdataFeed.text;
                          scanned = true;
                          print('done');
                          //if (temp != qrdataFeed.text)
                          numberOfClasses += 1;
                          print('$numberOfClasses is printed');
                        });
                        await tutor_details.doc(uid).set({
                          'Subjects': {'$subject': numberOfClasses},
                        }, SetOptions(merge: true));
                      }
                    },

Posting the @NisanthReddy's solution as a Community Wiki for visibility.将 @NisanthReddy 的解决方案发布为社区 Wiki 以提高知名度。

A better architecture would be to have one class at the root to handle all your transactions and to also notify any listener in your code.更好的架构是在根目录下使用一个 class 来处理所有事务并通知代码中的任何侦听器。

You should always call this class to update and you can add listeners to this class from any widget you want.您应该始终调用此 class 进行更新,您可以从您想要的任何小部件向此 class 添加侦听器。 This way you will have everything Firestore-related in one place.这样,您将在一个地方拥有与 Firestore 相关的所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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