Flutter 如何从flatter';中的所选项目获取文档Id;s下拉菜单
我想从firestore获取一个文档Id当用户在Flatter中选择下拉菜单的下拉项时,我尝试了一些逻辑,但它们对我不起作用 这是我的代码Flutter 如何从flatter';中的所选项目获取文档Id;s下拉菜单,flutter,dart,google-cloud-firestore,drop-down-menu,Flutter,Dart,Google Cloud Firestore,Drop Down Menu,我想从firestore获取一个文档Id当用户在Flatter中选择下拉菜单的下拉项时,我尝试了一些逻辑,但它们对我不起作用 这是我的代码 Widget cityDropDown() { return new StreamBuilder<QuerySnapshot>( stream: Firestore.instance.collection('city').snapshots(), builder: (context, snapshot) {
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());
},
);
},
);
},
);
}
Widget-cityDropDown(){
返回新的StreamBuilder(
流:Firestore.instance.collection('city').snapshots(),
生成器:(上下文,快照){
如果(!snapshot.hasData)
返回常数中心(
子项:常量CupertinoActivityIndicator(),
);
返回DropdownButtonFormField(
装饰:输入装饰(标签文本:“城市”),
项目:snapshot.data.documents.map((DocumentSnapshot文档){
返回新的DropdownMenuItem(
值:document.data['name'],
子项:新文本(document.data['name']),
);
}).toList(),
验证器:(值){
如果(值==null){
返回“请选择您的城市”;
}
返回null;
},
一旦更改:(值){
设定状态(
() {
_城市=价值;
_社会=零;
_建筑=空;
_houseNumber=null;
_userStatus=null;
_职业状态=空;
FocusScope.of(context.requestFocus(FocusNode());
},
);
},
);
},
);
}
所有这些都是psuedo代码
在本节中:
value: document.data['name'],
child: new Text(document.data['name']),
您可以将值改为文档本身,而不是名称的文本值。然后您可以在代码的这一部分中添加类似的内容:
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());
},
);
},
所有这些都是伪代码
在本节中:
value: document.data['name'],
child: new Text(document.data['name']),
您可以将值改为文档本身,而不是名称的文本值。然后您可以在代码的这一部分中添加类似的内容:
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());
},
);
},
我想你试着说我必须将value和text的值改为document而不是document.data['name'],然后它不会在下拉列表中显示城市列表,而是显示“DocumentSnapshot”的实例如果我理解您的解决方案有误,请告诉我。我想您可能会试图说我必须将value和text的值改为document而不是document.data['name'],然后它不会在下拉列表中显示城市列表,它会显示“DocumentSnapshot'实例”。如果我理解您的解决方案有误,请告诉我。