I have this flutter project that has home screen. I have sent http get request to api URL and I need to get data from snapShot. But snapShot.hasData returns false.
Console output when I print response body.
Performing hot reload...
Syncing files to device sdk gphone64 x86 64...
I/flutter ( 4384): NoSuchMethodError: Class 'MappedListIterable<Map<String, dynamic>, User>' has no instance method 'tolist'.
I/flutter ( 4384): Receiver: Instance of 'MappedListIterable<Map<String, dynamic>, User>'
I/flutter ( 4384): Tried calling: tolist()
Reloaded 1 of 671 libraries in 1,336ms (compile: 141 ms, reload: 534 ms, reassemble: 567 ms).
D/EGL_emulation( 4384): app_time_stats: avg=271975.19ms min=271975.19ms max=271975.19ms count=1
I/flutter ( 4384): [{"name":"James Calton","city":"London","image":"https://randomuser.me/api/portraits/men/78.jpg"},{"name":"Don Quixote","city":"Madrid","image":"https://randomuser.me/api/portraits/men/74.jpg"},{"name":"Joan of Arc","city":"Paris","image":"https://randomuser.me/api/portraits/men/76.jpg"},{"name":"Rosa Park","city":"Alabama","image":"https://randomuser.me/api/portraits/men/75.jpg"},{"name":"James Calton","city":"London","image":"https://randomuser.me/api/portraits/men/78.jpg"},{"name":"Don Quixote","city":"Madrid","image":"https://randomuser.me/api/portraits/men/74.jpg"},{"name":"James Calton","city":"London","image":"https://randomuser.me/api/portraits/men/78.jpg"},{"name":"Don Quixote","city":"Madrid","image":"https://randomuser.me/api/portraits/men/74.jpg"},{"name":"James Calton","city":"London","image":"https://randomuser.me/api/portraits/men/78.jpg"},{"name":"Don Quixote","city":"Madrid","image":"https://randomuser.me/api/portraits/men/79.jpg"},{"name":"James Calton","city":"London","image":"https://rand
I/flutter ( 4384): NoSuchMethodError: Class 'MappedListIterable<Map<String, dynamic>, User>' has no instance method 'tolist'.
I/flutter ( 4384): Receiver: Instance of 'MappedListIterable<Map<String, dynamic>, User>'
I/flutter ( 4384): Tried calling: tolist()
D/EGL_emulation( 4384): app_time_stats: avg=1965.76ms min=1965.76ms max=1965.76ms count=1
Main code that checks if snapShot.hasData :
Expanded(child: FutureBuilder(
future: apiService.getUsers(),
builder: (context, snapShot){
print(snapShot.error);
if(snapShot.hasData)
{
return ListView.builder(
itemCount: snapShot.data?.length,
itemBuilder: (context , index){
return GestureDetector(
//onTap: (){},
child: Container(
margin: EdgeInsets.symmetric(horizontal: 10,vertical: 10),
padding: EdgeInsets.symmetric(horizontal: 10,vertical: 10),
child: ListTile(
title: Text('test', style: TextStyle(fontSize: 18, color: Colors.black),) //snapShot.data[index].name
),
),
);
},
);
}else{
return Center(
child: CircularProgressIndicator(),
}
The code that sends the http get request to the api url and checks if status code is 200 :
class RestAPIService{
String apiUrl = 'https://mocki.io/v1/ed0c6388-7a27-4c27-942b-f1b6b358178e';
//// Future method to Get all users from API url | Returns > Response/Error
Future<List<User>> getUsers() async{
final response = await http.get(Uri.parse(apiUrl));
print(response.body);
//print(response.body);
//// checking if the request is success with statusCode (200 = OK)
if(response.statusCode == 200){
//print(response.body);
getUsersList(response.body);
}else{
throw Exception('Unable to fetch data');
}
return getUsers();
}
//// Convert response body => user object list
List<User> getUsersList(String responseBody)
{
final parsedBody = json.decode(responseBody).cast<Map<String , dynamic>>();
return parsedBody.map<User>((json) => User.fromJson(json)).tolist();
}
}
All dart files : https://drive.google.com/drive/folders/1bcDV7eR_n7WgjCcmI1yKZoKRcAQhEylV?usp=sharing
I dont know why snapShot has no data, but response of requests has data.
CodePudding user response:
All i did was changing this
builder: (context, snapShot){
to this :
builder: (context, AsyncSnapshot snapShot){
and replacing tolist with toList as suggested by @pskink