Hi am trying to display the list of info from api i have created api and model and provider classes respectively when i try to access the api am getting this "List' is not a subtype of type 'Map<String, dynamic>"
where my api response is given below
[
{
"id": 1,
"username": "naveen",
"email": "[email protected]",
"scheduledDate": "15-09-2022",
"remarks": "demo",
"createdAt": "2022-09-14T23:57:09.344Z",
"updatedAt": "2022-09-14T23:57:09.344Z"
},
{
"id": 2,
"username": "naveen",
"email": "[email protected]",
"scheduledDate": "16-09-2022",
"remarks": "demo",
"createdAt": "2022-09-14T23:57:17.756Z",
"updatedAt": "2022-09-14T23:57:17.756Z"
}
]
and my model class
class ScheduleListModel {
int? id;
String? username;
String? email;
String? scheduledDate;
String? remarks;
String? createdAt;
String? updatedAt;
ScheduleListModel(
{this.id,
this.username,
this.email,
this.scheduledDate,
this.remarks,
this.createdAt,
this.updatedAt});
ScheduleListModel.fromJson(Map<String, dynamic> json) {
id = json['id'];
username = json['username'];
email = json['email'];
scheduledDate = json['scheduledDate'];
remarks = json['remarks'];
createdAt = json['createdAt'];
updatedAt = json['updatedAt'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['id'] = id;
data['username'] = username;
data['email'] = email;
data['scheduledDate'] = scheduledDate;
data['remarks'] = remarks;
data['createdAt'] = createdAt;
data['updatedAt'] = updatedAt;
return data;
}
}
and provider class
class ScheduleListProvider extends ChangeNotifier {
bool isBack = false;
late ScheduleListModel scheduleListModel;
ScheduleListModel get getScheduleListModel => scheduleListModel;
Future<void> getAllScheduleListData() async {
getAllSchedule().then((response) => {
if (response!.statusCode == 200)
{
scheduleListModel =
ScheduleListModel.fromJson(json.decode(response.body)),
notifyListeners(),
}
});
}
}
this is my api call
Future<http.Response?> getAllSchedule() async {
late SharedPreferences logindata;
logindata = await SharedPreferences.getInstance();
late String? token = logindata.getString('token');
http.Response? response;
try {
response = await http.get(Uri.parse(Config.getAllScheduleAPI), headers: {
HttpHeaders.contentTypeHeader: "application/json",
'x-access-token': '$token',
});
} catch (e) {
log(e.toString());
}
return response;
}
when am trying to do api call am getting exception as
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'
help me to solve this
thank you very much in advance
CodePudding user response:
Your api response is List<ScheduleListModel>
, not a single ScheduleListModel. Thats why
ScheduleListModel.fromJson(json.decode(response.body)),
throws this error message.
Depending on what you want to do with the list, you should change getAllScheduleListData()
to something which can work with a list of ScheduleListModel
.
CodePudding user response:
Try this:
class ScheduleListProvider extends ChangeNotifier {
bool isBack = false;
List<ScheduleListModel> scheduleList = [];
Future<void> getAllScheduleListData() async {
getAllSchedule().then((response) => {
if (response!.statusCode == 200) {
json.decode(response.body).forEach((element){
scheduleList.add(ScheduleListModel.fromJson(element));
});
notifyListeners(),
}
});
}
}