Hey Guys i am facing a problem i am retrieving data from an api with this code
class carLists{
carLists();
getCarsFromNetwork(String jsonUrl) async {
List<Cars> list;
String link = jsonUrl;
var res = await http.get(Uri.encodeFull(link),headers: {"Accept":"Application/json"});
if (res.statusCode == 200) {
var data = json.decode(res.body);
var list = data["cars"] as List;
List<Cars> imagesList = list.map((i) => Cars.fromJson(i)).toList();
return imagesList;
}
}
List<DropdownMenuItem> getCars(List<Cars> carPlates){
List<DropdownMenuItem<String>> ordersType = new List<DropdownMenuItem<String>>();
for(var i = 0 ; i<carPlates.length;i++ ) {
ordersType.add(DropdownMenuItem(
value: carPlates[i].carPlate, child: Text(carPlates[i].carPlate),));
}
return ordersType;
}
}
And implementing a dropdownbutton using these two functions to get the data and return the dropdownmenuitem list :`
Future<List<Cars>> foo(String link) async{
carPlates = await carLists().getCarsFromNetwork(link);
print(carPlates.length);
return carPlates;
}
getItems(List values) {
List<DropdownMenuItem<String>> ordersType = new List<
DropdownMenuItem<String>>();
for (var i = 0; i > values.length; i++) {
ordersType.add(DropdownMenuItem(value: values[i], child: values[i],));
return ordersType;
}
}
`and using this code to populate the DropdownButton
Container(
child: FutureBuilder(
future: foo(url) ,
builder: (context, snapshot){
if(snapshot.hasData)
{
return new DropdownButton(
iconDisabledColor: Colors.black,
isExpanded: true,
icon: Icon(FontAwesomeIcons.arrowCircleDown),
iconSize: 14,
style: TextStyle(fontSize: 16, color: Colors.black),
iconEnabledColor: Colors.deepOrange,
items: carLists().getCars(carPlates),
value: dropTyreBrand,
onChanged: (val) {
setState(() {
dropTyreBrand = val;
});
},
);
}else{
return CircularProgressIndicator();
}
}
),
) ,
the values are populated and everything is fine but when i select a value from the dropdown it crashes with this error
I/flutter ( 4578): The following assertion was thrown building FutureBuilder>(dirty, state: I/flutter ( 4578): _FutureBuilderState>#f394d): I/flutter ( 4578): 'package:flutter/src/material/dropdown.dart': Failed assertion: line 608 pos 15: 'items == null || I/flutter ( 4578): items.isEmpty || value == null || items.where((DropdownMenuItem item) => item.value == I/flutter ( 4578): value).length == 1': is not true. I/flutter ( 4578): I/flutter ( 4578): Either the assertion indicates an error in the framework itself, or we should provide substantially I/flutter ( 4578): more information in this error message to help you determine and fix the underlying cause. I/flutter ( 4578): In either case, please report this assertion by filing a bug on GitHub: I/flutter ( 4578): https://github.com/flutter/flutter/issues/new?template=BUG.md I/flutter ( 4578): I/flutter ( 4578): When the exception was thrown, this was the stack: I/flutter ( 4578): #2 new DropdownButton