如何以列表的形式从类似于http://lovemonster.my.id/hospital的模型中获取数据到我的BLoC构建器中?当我获取Type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'
时,我收到了一条错误消息,这是我的提供程序:
class ApiProvider {
final Dio _dio = Dio();
final String _url = 'http://lovemonster.my.id/hospital';
Future<HospitalListModel> fetchHospitalList() async {
try {
Response response = await _dio.get(_url);
return HospitalListModel.fromJson(response.data);
} catch (error, stacktrace) {
print("Exception occurred: $error stackTrace: $stacktrace");
return Future.error("");
}
}
}
这是我的仓库
class ApiRepository {
final _provider = ApiProvider();
Future<HospitalListModel> fetchHospitalList() {
return _provider.fetchHospitalList();
}
}
class NetworkError extends Error {}
这是模型,如果你想知道它看起来像什么:
class HospitalListModel {
int? id;
String? title;
String? content;
String? image;
String? phone;
String? coordinates;
String? website;
String? createdAt;
String? updatedAt;
HospitalListModel({
this.id,
this.title,
this.content,
this.image,
this.phone,
this.coordinates,
this.website,
this.createdAt,
this.updatedAt,
});
HospitalListModel.fromJson(Map<String, dynamic> json) {
id = json['id'] as int?;
title = json['title'] as String?;
content = json['content'] as String?;
image = json['image'] as String?;
phone = json['phone'] as String?;
coordinates = json['coordinates'] as String?;
website = json['website'] as String?;
createdAt = json['created_at'] as String?;
updatedAt = json['updated_at'] as String?;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> json = <String, dynamic>{};
json['id'] = id;
json['title'] = title;
json['content'] = content;
json['image'] = image;
json['phone'] = phone;
json['coordinates'] = coordinates;
json['website'] = website;
json['created_at'] = createdAt;
json['updated_at'] = updatedAt;
return json;
}
}
有人能告诉我如何从我的API中获取数据吗
1条答案
按热度按时间kb5ga3dv1#
从这里打电话
示例: