当用户在flutter中选择下拉菜单的下拉项时,我想从Firestore中获取文档ID,我尝试了一些逻辑,但对我来说不起作用。
这是我的代码
Widget cityDropDown() {
return new StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('city').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData)
return const Center(
child: const CupertinoActivityIndicator(),
);
return DropdownButtonFormField<String>(
decoration: InputDecoration(labelText: 'City'),
items: snapshot.data.documents.map((DocumentSnapshot document) {
return new DropdownMenuItem<String>(
value: document.data['name'],
child: new Text(document.data['name']),
);
}).toList(),
validator: (value) {
if (value == null) {
return 'Please choose your city';
}
return null;
},
onChanged: (value) {
setState(
() {
_city = value;
_society = null;
_building = null;
_houseNumber = null;
_userStatus = null;
_occupancyStatus = null;
FocusScope.of(context).requestFocus(FocusNode());
},
);
},
);
},
);
}
答案 0 :(得分:0)
所有这些都是伪代码 在本节中:
value: document.data['name'],
child: new Text(document.data['name']),
您可以使value本身是文档本身,而不是名称的文本值。然后,您可以在代码的这一部分中添加如下内容:
onChanged: (value) {
**aPersistentVariablePreviouslyDeclared = value.id;**
setState(
() {
_city = value.data["name"];
_society = null;
_building = null;
_houseNumber = null;
_userStatus = null;
_occupancyStatus = null;
FocusScope.of(context).requestFocus(FocusNode());
},
);
},