【发布时间】:2019-05-31 08:50:18
【问题描述】:
DropdownButton Value does not update even after selecting different items.
如果默认值为空,则显示错误消息,如果我传递任何默认值(非空),则它永远不会更改为其他选定的值。
currentCategory 被设置为 DropdownButton 的默认值。
StreamBuilder<QuerySnapshot>(
stream: Firestore.instance
.collection('categories')
.snapshots(),
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot) {
currentCategory = snapshot.data.documents[0];
return DropdownButtonHideUnderline(
child:
new DropdownButtonFormField<DocumentSnapshot>(
value: currentCategory,
onChanged: (DocumentSnapshot newValue) {
setState(() {
currentCategory = newValue;
});
print(currentCategory.data['name']);
},
onSaved: (DocumentSnapshot newValue) {
setState(() {
currentCategory = newValue;
});
},
items: snapshot.data.documents
.map((DocumentSnapshot document) {
return new DropdownMenuItem<DocumentSnapshot>(
value: document,
child: Text(
document.data['name'],
),
);
}).toList(),
),
);
}),
帮我解决这个问题。 提前致谢。
【问题讨论】:
-
覆盖
initState()并创建一个stream变量,然后在流生成器中设置流值。
标签: flutter google-cloud-firestore dropdownbutton stream-builder