I am using JSON API to get the data into my dropdownlist in Flutter. But whenever I am clicking on the dropdownlist the data does not appear in the list.
Following is my code:
class ItemSelection extends StatefulWidget{
@override
_ItemSelectionState createState() => _ItemSelectionState();
}
class _ItemSelectionState extends State<ItemSelection> {
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
body: Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
padding: EdgeInsets.all(20.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text("Dropdown",
style: TextStyle(
fontSize: 20.0,
),
textAlign: TextAlign.center,
),
SizedBox(
height: 20.0,
),
DropDownField(
controller: cityselected,
hintText: "Select an item",
enabled: true,
itemsVisibleInDropdown: 5,
items: items,
onValueChanged: (value){
setState(() {
selectedcity = value;
});
}
)
],
),
),
);
}
}
final List<String> items = [];
Future getitem() async {
var response = await http.get(
"basic json url here",
headers: {
"Accept": "application/json"
}
);
if(response.statusCode == 200){
List<GetItem> getitem = getItemFromJson(response.body);
for(int i =0; i< getitem.length; i++){
items.add(getitem[i].itemName);
}
print(items);
}
}
GetItem.dart:
import 'dart:convert';
List<GetItem> getItemFromJson(String str) => List<GetItem>.from(json.decode(str).map((x) => GetItem.fromJson(x)));
String getItemToJson(List<GetItem> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class GetItem {
GetItem({
this.sLNo,
this.itemName,
this.category,
});
String sLNo;
String itemName;
String category;
factory GetItem.fromJson(Map<String, dynamic> json) => GetItem(
sLNo: json["S/L NO"],
itemName: json["Item Name"] == null ? null : json["Item Name"],
category: json["Category"] == null ? null : json["Category"],
);
Map<String, dynamic> toJson() => {
"S/L NO": sLNo,
"Item Name": itemName == null ? null : itemName,
"Category": category == null ? null : category,
};
}
As soon as I click on the dropdownlist I want the data coming from the JSON API to show in there. Can someone help me with this please?
