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(),
),
);
}),
帮我解决这个问题。提前致谢。