【问题标题】:Pulling data from QuerySnapshot in flutter在颤振中从 QuerySnapshot 中提取数据
【发布时间】:2021-08-02 20:18:37
【问题描述】:

我需要从 Firestore 中的文档填充 DropdownButton。我可以检索数据。当我查看 snapshot.data 时,我看到了 2 条记录,这是我希望看到的。在下面的代码中,只要我注释掉代码sn-p就可以了,你可以看到。

    Container(
                      child: StreamBuilder(
                          //stream: _firestoreService.getAgency(),
                          stream: _db.collection('agency').snapshots(),
                          builder: (BuildContext context, AsyncSnapshot snapshot) {
                            if (snapshot.data == null) {
                              return Center(
                                child: CircularProgressIndicator(),
                              );
                            } else {
                              //var length = snapshot.data.docs.length;
                              //print('length: ' + length);
                              return new DropdownButton<String>(
                                hint: new Text("Select Agency"),
                                value: _currentAgency,
    /*  <<<< The code below is where I am having problems
                                //onChanged: changedDropDownState,
                                **items: snapshot.data.docs.map((Map map) {
                                  return new DropdownMenuItem<String>(
                                    value: map["name"].toString(),
                                    child: new Text(
                                    map["name"],
                                    ),
                                  );
                                }).toList(),**
*/
                              );
                            }
                            ;
                          }),
                    ),

当我取消注释代码并运行应用程序时,我收到此错误:

======== Exception caught by widgets library =======================================================
The following _TypeError was thrown building StreamBuilder<QuerySnapshot>(dirty, state: _StreamBuilderBaseState<QuerySnapshot, AsyncSnapshot<QuerySnapshot>>#d9273):
type '(Map<dynamic, dynamic>) => DropdownMenuItem<String>' is not a subtype of type '(QueryDocumentSnapshot) => dynamic' of 'f'

我想要完成的是使用文档 ID 填充 value: 属性,但我在 snapshot.data 中看不到它。我想做的另一件事是使用来自 snapshot.data 的一些值填充 child: 属性。

我该怎么做?

【问题讨论】:

    标签: flutter flutter-dropdownbutton


    【解决方案1】:

    你的问题在这里

    items: snapshot.data.docs.map(( ISSUE HERE===>Map map) {
      return new DropdownMenuItem<String>(
      value: map["name"].toString(),
      child: new Text(
      map["name"],
         ),
       );
     }).toList(),
    

    docs 的值是 QueryDocumentSnapshot,而不是输入 Map,这就是您收到错误消息的原因。

    改为这样。

    items: snapshot.data.docs.map((documentSnapshot) {
      return new DropdownMenuItem<String>(
      value: documentSnapshot["name"].toString(),
      child: new Text(
      documentSnapshot["name"],
         ),
       );
     }).toList(),
    

    【讨论】:

      【解决方案2】:

      我发现一个帖子说使用 .forEach 代替 map (Snapshot code inside StreamBuilder does not get executed when receiving data from Firebase FireCloud in Flutter)。

      这消除了错误,但 DropdownButton 是静态的,不会下拉或让我点击它。这是我的 DropdownButton 的新代码:

      return new DropdownButton<String>(
                                  hint: new Text("Select Agency"),
                                  value: _currentAgency,
                                  //onChanged: changedDropDownState,
                                  items: snapshot.data.docs.forEach((document) {
                                    return new DropdownMenuItem<String>(
                                      value: document.data()['name'],
                                      child: new Text(document.data()['name']),
                                    );
                                  }),
                                );
      

      数据在那里,我在调试时可以看到它。我也想获取 documentId,但在调试器中没有看到。

      如何使 DropdownButton 处于活动状态以及如何将 documentId 添加到 value: 属性?

      【讨论】:

        猜你喜欢
        • 2021-03-26
        • 2020-05-22
        • 2019-08-27
        • 2022-07-27
        • 2019-11-21
        • 2019-04-20
        • 2020-11-17
        • 2022-10-06
        • 2021-10-30
        相关资源
        最近更新 更多