0

我需要从 Firestore 中的文档填充 DropdownButton。我可以检索数据。当我查看 snapshot.data 时,我看到了 2 条记录,这是我希望看到的。在下面的代码中,只要我注释掉您所看到的代码片段,一切正常。

    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: 属性。

我该怎么做呢?

4

2 回答 2

0

你的问题在这里

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

docsisQueryDocumentSnapshot而不是 type的值,Map这就是您收到错误消息的原因。

相反,将其更改为此。

items: snapshot.data.docs.map((documentSnapshot) {
  return new DropdownMenuItem<String>(
  value: documentSnapshot["name"].toString(),
  child: new Text(
  documentSnapshot["name"],
     ),
   );
 }).toList(),
于 2021-05-12T15:43:30.127 回答
0

我发现一篇帖子说使用 .forEach 而不是 map (在 Flutter 中从 Firebase FireCloud 接收数据时,StreamBuilder 中的快照代码不会被执行)。

这消除了错误,但 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-05-12T17:06:11.993 回答