Flutter 如何修复使用Firestore填充下拉按钮的错误
我想用Firestore填充一个下拉按钮,我可以得到数据,但我不能把它放在下拉菜单上 我已经发现了问题,这是下拉按钮的项目列表,但我不明白发生了什么 当我在“items”上设置valor null时,它可以工作(不显示错误),但不显示任何内容(显然) 这是我的密码Flutter 如何修复使用Firestore填充下拉按钮的错误,flutter,google-cloud-firestore,Flutter,Google Cloud Firestore,我想用Firestore填充一个下拉按钮,我可以得到数据,但我不能把它放在下拉菜单上 我已经发现了问题,这是下拉按钮的项目列表,但我不明白发生了什么 当我在“items”上设置valor null时,它可以工作(不显示错误),但不显示任何内容(显然) 这是我的密码 StreamBuilder<QuerySnapshot>( stream: Firestore.instance .collection('usuarios/$id/ubic
StreamBuilder<QuerySnapshot>(
stream: Firestore.instance
.collection('usuarios/$id/ubications')
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
const Text("Error");
} else {
List<DropdownMenuItem> ubicationItems = [];
print(snapshot.data.documents.length);
for (int i = 0; i < snapshot.data.documents.length; i++) {
DocumentSnapshot snap = snapshot.data.documents[i];
//_accountType.add(snap['ubication']);
ubicationItems.add(DropdownMenuItem(
child: Text(
snap['ubication'],
),
value: "${snap['ubication']}",
));
}
print(ubicationItems.length); //it has the data
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
DropdownButton(
items: ubicationItems,
onChanged: (selectedType) {
print('$selectedType');
setState(() {
selectedType = selectedUbication;
});
},
value: selectedUbication,
isExpanded: true,
hint: new Text("ubication"),
),
],
);
}
}),
StreamBuilder(
流:Firestore.instance
.collection('usuarios/$id/ubicions'))
.snapshots(),
生成器:(上下文,快照){
如果(!snapshot.hasData){
常量文本(“错误”);
}否则{
列表ubicationItems=[];
打印(快照、数据、文档、长度);
对于(int i=0;i
这就是错误:I/flatter(14877):引发了另一个异常:“package:flatter/src/material/dropdown.dart”:失败的断言:第609行位置15:“items==null | | | items.isEmpty | | value==null | | items.where((DropdownMenuItem=>item.value==value)。length==1”:不为真 我使用了以下示例,如指南:
我希望有人能帮我,谢谢。哪一个是609号线?您在哪里设置
selectedUbication
的值?我没有第609行,我在类中声明了selectedUbication,在函数setState()中声明了valor change print(selectedUbication)的输出是什么?断言告诉您items==null、empty或value==null,或者我认为所选项的长度==1。问题是我将变量selectedUbication声明为字符串,并将其更改为var,现在可以工作了!伟大的考虑回答和接受你自己的问题,所以别人知道你修了它,或者可以找到答案,如果他们有同样的问题,哪一个是第609行?您在哪里设置selectedUbication
的值?我没有第609行,我在类中声明了selectedUbication,在函数setState()中声明了valor change print(selectedUbication)的输出是什么?断言告诉您items==null、empty或value==null,或者我认为所选项的长度==1。问题是我将变量selectedUbication声明为字符串,并将其更改为var,现在可以工作了!伟大的考虑回答和接受你自己的问题,所以别人知道你修了它,或者如果他们有同样的问题也能找到答案。