flutter 类型“List< dynamic>”不是类型“Map〈String,dynamic>”的子类型

8iwquhpp  于 2022-12-19  发布在  Flutter
关注(0)|答案(1)|浏览(260)

如何以列表的形式从类似于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中获取数据吗

kb5ga3dv

kb5ga3dv1#

// To parse this JSON data, do
//
//     final hospitalListModel = hospitalListModelFromJson(jsonString);

import 'dart:convert';

List<HospitalListModel> hospitalListModelFromJson(String str) => List<HospitalListModel>.from(json.decode(str).map((x) => HospitalListModel.fromJson(x)));

String hospitalListModelToJson(List<HospitalListModel> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class HospitalListModel {
    HospitalListModel({
        this.id,
        this.title,
        this.content,
        this.image,
        this.phone,
        this.coordinates,
        this.website,
        this.createdAt,
        this.updatedAt,
    });

    dynamic id;
    dynamic title;
    dynamic content;
    dynamic image;
    dynamic phone;
    dynamic coordinates;
    dynamic website;
    dynamic createdAt;
    dynamic updatedAt;

    factory HospitalListModel.fromJson(Map<String, dynamic> json) => HospitalListModel(
        id: json["id"],
        title: json["title"],
        content: json["content"],
        image: json["image"],
        phone: json["phone"],
        coordinates: json["coordinates"],
        website: json["website"],
        createdAt: json["created_at"],
        updatedAt: json["updated_at"],
    );

    Map<String, dynamic> toJson() => {
        "id": id,
        "title": title,
        "content": content,
        "image": image,
        "phone": phone,
        "coordinates": coordinates,
        "website": website,
        "created_at": createdAt.toIso8601String(),
        "updated_at": updatedAt.toIso8601String(),
    };
}

从这里打电话

final hospitalListModel = hospitalListModelFromJson(jsonString);

示例:

Future<List<HospitalListModel>> fetchHospitalList() async {
    try {
      Response response = await _dio.get(_url);
      return hospitalListModelFromJson(response.body);
    } catch (error, stacktrace) {
      print("Exception occurred: $error stackTrace: $stacktrace");
      return Future.error("");
    }
  }

相关问题