我正在尝试点击一个laravel并在flutter中显示它。
[
{
"doctor_name": "abhishek",
"username": "abhishek",
"uid": "aLSb7ebMfsfAxybrwq21kXjkcJM2",
"fees": 500,
"speciality": "Oncologist"
},
{
"username": "amanboi",
"uid": "wpTQALmZd5Yr5BVQyblNstjet1A3",
"fees": 500,
"speciality": "Oncologist",
"doctor_name": "aman"
}
]当我试图将它映射到我的模型时,我得到了错误。这是我的模型的样子
class Doctor {
final String uid;
final int fee;
final String doctor_name;
final String speciality;
Doctor({this.uid, this.fee, this.doctor_name, this.speciality});
factory Doctor.fromJson(Map<String, dynamic> json) {
return Doctor(
uid: json['userId'],
fee: json['fee'],
doctor_name: json['doctor_name'],
speciality: json['speciality']
);
}
}这是我的函数
Future<Doctor> doctorlist(String speciality ) async {
final response = await http.post('http://192.168.0.101:8080/querysnapshot', body: {'speciality': speciality});
print('got response successfully');
if (response.statusCode == 200) {
print(response.body);
return Doctor.fromJson(json.decode(response.body));
} else {
throw Exception('Failed to load album');
}
}我收到以下错误:
type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'发布于 2020-06-26 18:06:24
您正在将从API获得的List传递到模型中。返回列表。
Future<List> doctorlist(String speciality) async {
final response = await http.post('http://192.168.0.101:8080/querysnapshot', body: {'speciality': speciality});
print('got response successfully');
if (response.statusCode == 200) {
print(response.body);
return json.decode(response.body);
} else {
throw Exception('Failed to load album');
}
}在你的ListView中,你可以这样做
ListView.builder(
itemBuilder: (BuildContext context, int index) {
Doctor doctor = Doctor.fromJson(doctorList[index]);
return Text(doctor.name);
},
);https://stackoverflow.com/questions/62592394
复制相似问题