In flutter i have an array, model and service. Like this
Array
[{"id":1,"name":"Elizabeth Mayer","email":"legros.murphy@example.com","language":"en","city":"North Alfredo","latitude":52.608474,"longitude":-148.435351,"created_at":"2022-01-13T22:59:09.000000Z","updated_at":"2022-01-13T22:59:09.000000Z"},{"id":2,"name":"Jedidiah Wilkinson","email":"vfahey@example.net","language":"en","city":"Uptonport","latitude":-10.587622,"longitude":-50.709927,"created_at":"2022-01-13T22:59:09.000000Z","updated_at":"2022-01-13T22:59:09.000000Z"},{"id":3,"name":"Ms. Beth Kozey","email":"hartmann.quincy@example.com","language":"en","city":"Lake Gregorymouth","latitude":-85.088104,"longitude":-41.870917,"created_at":"2022-01-13T22:59:09.000000Z","updated_at":"2022-01-13T22:59:09.000000Z"},{"id":4,"name":"Zelda Gaylord","email":"uthiel@example.net","language":"en","city":"East Jayden","latitude":-16.937626,"longitude":-46.935818,"created_at":"2022-01-13T22:59:09.000000Z","updated_at":"2022-01-13T22:59:09.000000Z"},{"id":5,"name":"Zetta Nolan","email":"orn.ellsworth@example.net","language":"es","city":"Littlefort","latitude":-70.983631,"longitude":114.926832,"created_at":"2022-01-13T22:59:09.000000Z","updated_at":"2022-01-13T22:59:09.000000Z"},{"id":6,"name":"Myron Bechtelar II","email":"nikolaus.aliyah@example.com","language":"en","city":"Nyabury","latitude":-24.464843,"longitude":-144.472216,"created_at":"2022-01-13T22:59:09.000000Z","updated_at":"2022-01-13T22:59:09.000000Z"},{"id":7,"name":"Dr. Adella Hermiston","email":"sporer.katelin@example.com","language":"es","city":"Rauburgh","latitude":-18.944024,"longitude":-115.838682,"created_at":"2022-01-13T22:59:09.000000Z","updated_at":"2022-01-13T22:59:09.000000Z"},{"id":8,"name":"Malinda Pfeffer","email":"colin.walker@example.com","language":"en","city":"West Domenickborough","latitude":57.215318,"longitude":9.643726,"created_at":"2022-01-13T22:59:09.000000Z","updated_at":"2022-01-13T22:59:09.000000Z"},{"id":9,"name":"Chanelle Rau","email":"price.vernice@example.org","language":"es","city":"Wizaside","latitude":-12.169768,"longitude":83.807733,"created_at":"2022-01-13T22:59:09.000000Z","updated_at":"2022-01-13T22:59:09.000000Z"},{"id":10,"name":"Jarod Abshire I","email":"levi20@example.net","language":"es","city":"Hicklefurt","latitude":52.215627,"longitude":37.358283,"created_at":"2022-01-13T22:59:09.000000Z","updated_at":"2022-01-13T22:59:09.000000Z"}]
Model
class UserModel {
final String name;
final String email;
final String password;
final String language;
final String city;
final double latitude;
final double longitude;
UserModel(
{required this.name,
required this.email,
required this.password,
required this.language,
required this.city,
required this.latitude,
required this.longitude});
factory UserModel.fromJson(Map<String, dynamic> json) {
return UserModel(
name: json['name'],
email: json['email'],
password: json['password'],
language: json['language'],
city: json['city'],
latitude: json['latitude'],
longitude: json['longitude'],
);
}
}
Service
Future<UserModel> getAll() async {
var url = Uri.parse('http://10.0.2.2:80/users/all');
var response = await http.get(url);
if (response.statusCode == 200) {
return UserModel.fromJson(jsonDecode(response.body));
} else {
throw Exception('Error: ${response.statusCode}.');
}
}
The problem. When i try to get this service data, return this
List' is not a subtype of type 'Map<String, dynamic>
I know what´s the error, different list type, but i don't know exactly how fix them. I saw some similar questions in stackoverflow, but nothing shows a clear way to fix it.