首页 > 解决方案 > 将 DropdownButton 中的选定项目设置为从 firestore 文档中检索到的项目

问题描述

DropdownButtons在你们许多人的帮助下,我已成功添加到我的应用程序中。谢谢。

现在,当我从 firestore 文档填充屏幕时,我需要列表中的项目与文本窗口DropdownMenuItem中显示的文档中存储的值相匹配。DropdownButton我使用 Stream 来构建DropdownMenuItem列表。

 void changedDropDownAgency(String selected_agency) {
    setState(() {
      _currentAgency = selected_agency;
    });
    globals.selectedAgency = selected_agency;
  }

void initState() {
  super.initState();

      _dropDownState = getDropDownState();
      _currentState = _dropDownState[0].value;
    }

This is just the snippet that builds the DropdownButton.

Container(
                  child: StreamBuilder(
                      stream: _db.collection('agency').snapshots(),
                      builder: (BuildContext context, AsyncSnapshot snapshot) {
                        if (snapshot.data == null) {
                          return Center(
                            child: CircularProgressIndicator(),
                          );
                        } else {
                          return new DropdownButton<String>(
                            hint: new Text("Select Agency"),
                            value: _currentAgency,
                            onChanged: changedDropDownAgency,
                            items: snapshot.data.docs
                                .map<DropdownMenuItem<String>>((document) {
                              return new DropdownMenuItem<String>(
                                value: document.id,
                                child: new Text(document.data()['name']),
                              );
                            }).toList(),
                          );
                        }
                        ;
                      }),
                ),

我想我拥有所有适用于这个特定 DropdownButton 的代码。因此,当页面加载时,我需要将存储在文档中的代理显示在 DropdownButton 中。再次感谢!

标签: flutterflutter-dropdownbutton

解决方案


推荐阅读