I am getting the below error in Flutter when trying to dynamically form a List and display the same in the UI.
'Expected a value of type 'List', but got one of type '({bool growable}) => List''
Below is how I form the List:
if (response.statusCode == 200) {
List items = jsonDecode(response.body)['result'].toList;
List<Contract> tempContractResults = [];
for (int i = 0; i < items.length; i ) {
Contract temp = Contract.fromJson(jsonDecode(response.body)['result'][i]);
tempContractResults.add(temp);
}
List<Contract> contractResults = tempContractResults;
Object class:
class Contract {
final String contractId;
Contract(
{this.contractId});
factory Contract.fromJson(Map<String, dynamic> json) {
return Contract(
contractId: json['contractId'].toString(),
}
}
And below code to display the list -
child: FutureBuilder<List<Contract>>(
future: futureAlbum,
builder: (context, snapshot) {
if (snapshot.hasData) {
if (snapshot.data != "") {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
return Text(snapshot.data[index].contractId);
},
);
Could anyone guide me on what could be the issue. I was thinking on the lines that the flutter considers static list and growable list differently and hence the issue. I tried some casting, but could not work it out.
CodePudding user response:
Maybe you forgot adding the parenthesis to .toList()
?
List items = jsonDecode(response.body)['result'].toList() // here;
(Would make a comment instead of an answer but my reputation is to low :p)