类型 'List<DropdownMenuItem<String>>' 不是类型转换中类型 'List<String>?' 的子类型


Widget locationList(String airportId) {
return Container(
child: StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance.collection("Airports").doc(widget.airportId).collection("Locations").snapshots(),
builder: (context, snapshot) {
if(snapshot.data == null){
return Container();
} else {
List<DropdownMenuItem<String>> locationItem = [];
for(int i=0;i<snapshot.data!.docs.length;i++){
DocumentSnapshot data = snapshot.data!.docs[i];
locationItem.add(
DropdownMenuItem<String>(
child: Text(
data["Location Name"],
),
value: "${data["Location Name"]}",
)
);
}
return Form(
key: _formKey,
child: Container(
alignment: Alignment.center,
height: 55,
width: 200,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Colors.black,width: 2)
),
child: DropdownButtonHideUnderline(
child: DropDownField(
value: value,
required: true,
items: locationItem,
enabled: true,
strict: false,
itemsVisibleInDropdown: 5,
onValueChanged: (value) {
setState(() {
this.value = value!;
locationId = value;
print(value);
print(locationId);
});
},
),
),
),
);
}
},
),
);
}

DropDownField中,必须提供List<String>items:的参数。

因此,首先将您的locatioItem类型更改为List<String>

List<String> locationItem = [];

然后更改for循环:

for (int i = 0; i < snapshot.data!.docs.length; i++) {
DocumentSnapshot data = snapshot.data!.docs[i];
locationItem.add(data["Location Name"]);
}

相关内容

  • 没有找到相关文章

最新更新