Reputation: 43
My api function:
Future<List<User>>fetchData() async {
var response = await http.get(
Uri.parse(link),
headers: headers);
var jsonData = jsonDecode(response.body);
List<User> users = [];
for (var u in jsonData) {
User user = User(u["name"], u["code"], u["id"]);
users.add(user);
}
print(users.length);
return users;
}
im getting this error in for loop. how can i fix it ? i need help. thanks for all
Upvotes: 1
Views: 32
Reputation: 12353
Your data is probably inside a list inside this json response. Look if you have a "data"
field in your response, then your for loop
would look something like this:
var u in jsonData["data"]
print our your jsonData
and let's see.
Upvotes: 1