我正在尝试敲打一个laravel api并以轻巧的方式显示它。
[
{
"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>'
答案 0 :(得分:1)
您要将从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);
},
);