flutter - 将 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 中。再次感谢!
解决方案
推荐阅读
- amazon-s3 - Python Boto3 Lambda 上传临时文件
- angularjs - AngularJS 不适用于“多个”标志
- scala - 添加列,同时保持 Apache Spark Scala 中现有列的相关性
- javascript - ReactJS,将道具从父组件传递给子组件?
- php - wc_update_order_item 未正确保存
- android - 如何在 Android 中使用 FFmpeg(v 4.0) HwAccell
- c++ - 如何在 C++ UWP 应用程序中使用静态库?
- asp.net - 调试到 asp.net Identity 开源代码
- c# - 窗口关闭后退出 WPF 应用程序
- c# - Unity 中的倒数计时器与 Photon 的 PUN 2