dart 解析我的嵌套JSON从strapi(localhost)到flutter block

zbq4xfa0  于 2023-07-31  发布在  Flutter
关注(0)|答案(1)|浏览(104)

这是我的json

{
    "data": [
        {
            "id": 2,
            "createdAt": "2023-07-14T13:04:00.394Z",
            "updatedAt": "2023-07-14T13:04:03.572Z",
            "publishedAt": "2023-07-14T13:04:03.564Z",
            "locale": "en",
            "Title": "AWS Job Ready course",
            "Description": "Amazon Web Services, Inc. is a subsidiary of Amazon that provides on-demand cloud computing platforms and APIs to individuals, companies, and governments, on a metered, pay-as-you-go basis. Oftentimes, clients will use this in combination with autoscaling.",
            "Order": 1,
            "CoverPic": {
                "id": 2,
                "name": "awscoursebanner.jpg",
                "alternativeText": null,
                "caption": null,
                "width": 650,
                "height": 345,
                "formats": {
                    "small": {
                        "ext": ".jpg",
                        "url": "/uploads/small_awscoursebanner_fbe397f8d5.jpg",
                        "hash": "small_awscoursebanner_fbe397f8d5",
                        "mime": "image/jpeg",
                        "name": "small_awscoursebanner.jpg",
                        "path": null,
                        "size": 15.2,
                        "width": 500,
                        "height": 265
                    },
                    "thumbnail": {
                        "ext": ".jpg",
                        "url": "/uploads/thumbnail_awscoursebanner_fbe397f8d5.jpg",
                        "hash": "thumbnail_awscoursebanner_fbe397f8d5",
                        "mime": "image/jpeg",
                        "name": "thumbnail_awscoursebanner.jpg",
                        "path": null,
                        "size": 5.15,
                        "width": 245,
                        "height": 130
                    }
                },
                "hash": "awscoursebanner_fbe397f8d5",
                "ext": ".jpg",
                "mime": "image/jpeg",
                "size": 22.33,
                "url": "/uploads/awscoursebanner_fbe397f8d5.jpg",
                "previewUrl": null,
                "provider": "local",
                "provider_metadata": null,
                "createdAt": "2023-07-14T13:03:24.800Z",
                "updatedAt": "2023-07-14T13:03:24.800Z"
            }
        }
    ],
    "meta": {
        "pagination": {
            "page": 1,
            "pageSize": 25,
            "pageCount": 1,
            "total": 1
        }
    }
}

字符串
下面是我从JSON到Dart站点生成的模型

class CoursesModel {
  List<Data>? data;
  Meta? meta;

  CoursesModel({this.data, this.meta});

  CoursesModel.fromJson(Map<String, dynamic> json) {
    if (json['data'] != null) {
      data = <Data>[];
      json['data'].forEach((v) {
        data!.add(new Data.fromJson(v));
      });
    }
    meta = json['meta'] != null ? new Meta.fromJson(json['meta']) : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.data != null) {
      data['data'] = this.data!.map((v) => v.toJson()).toList();
    }
    if (this.meta != null) {
      data['meta'] = this.meta!.toJson();
    }
    return data;
  }
}

class Data {
  int? id;
  String? createdAt;
  String? updatedAt;
  String? publishedAt;
  String? locale;
  String? title;
  String? description;
  int? order;
  CoverPic? coverPic;

  Data(
      {this.id,
      this.createdAt,
      this.updatedAt,
      this.publishedAt,
      this.locale,
      this.title,
      this.description,
      this.order,
      this.coverPic});

  Data.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    createdAt = json['createdAt'];
    updatedAt = json['updatedAt'];
    publishedAt = json['publishedAt'];
    locale = json['locale'];
    title = json['Title'];
    description = json['Description'];
    order = json['Order'];
    coverPic = json['CoverPic'] != null
        ? new CoverPic.fromJson(json['CoverPic'])
        : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['createdAt'] = this.createdAt;
    data['updatedAt'] = this.updatedAt;
    data['publishedAt'] = this.publishedAt;
    data['locale'] = this.locale;
    data['Title'] = this.title;
    data['Description'] = this.description;
    data['Order'] = this.order;
    if (this.coverPic != null) {
      data['CoverPic'] = this.coverPic!.toJson();
    }
    return data;
  }
}

class CoverPic {
  int? id;
  String? name;
  Null? alternativeText;
  Null? caption;
  int? width;
  int? height;
  Formats? formats;
  String? hash;
  String? ext;
  String? mime;
  double? size;
  String? url;
  Null? previewUrl;
  String? provider;
  Null? providerMetadata;
  String? createdAt;
  String? updatedAt;

  CoverPic(
      {this.id,
      this.name,
      this.alternativeText,
      this.caption,
      this.width,
      this.height,
      this.formats,
      this.hash,
      this.ext,
      this.mime,
      this.size,
      this.url,
      this.previewUrl,
      this.provider,
      this.providerMetadata,
      this.createdAt,
      this.updatedAt});

  CoverPic.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['name'];
    alternativeText = json['alternativeText'];
    caption = json['caption'];
    width = json['width'];
    height = json['height'];
    formats =
        json['formats'] != null ? new Formats.fromJson(json['formats']) : null;
    hash = json['hash'];
    ext = json['ext'];
    mime = json['mime'];
    size = json['size'];
    url = json['url'];
    previewUrl = json['previewUrl'];
    provider = json['provider'];
    providerMetadata = json['provider_metadata'];
    createdAt = json['createdAt'];
    updatedAt = json['updatedAt'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['name'] = this.name;
    data['alternativeText'] = this.alternativeText;
    data['caption'] = this.caption;
    data['width'] = this.width;
    data['height'] = this.height;
    if (this.formats != null) {
      data['formats'] = this.formats!.toJson();
    }
    data['hash'] = this.hash;
    data['ext'] = this.ext;
    data['mime'] = this.mime;
    data['size'] = this.size;
    data['url'] = this.url;
    data['previewUrl'] = this.previewUrl;
    data['provider'] = this.provider;
    data['provider_metadata'] = this.providerMetadata;
    data['createdAt'] = this.createdAt;
    data['updatedAt'] = this.updatedAt;
    return data;
  }
}

class Formats {
  Small? small;
  Small? thumbnail;

  Formats({this.small, this.thumbnail});

  Formats.fromJson(Map<String, dynamic> json) {
    small = json['small'] != null ? new Small.fromJson(json['small']) : null;
    thumbnail = json['thumbnail'] != null
        ? new Small.fromJson(json['thumbnail'])
        : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.small != null) {
      data['small'] = this.small!.toJson();
    }
    if (this.thumbnail != null) {
      data['thumbnail'] = this.thumbnail!.toJson();
    }
    return data;
  }
}

class Small {
  String? ext;
  String? url;
  String? hash;
  String? mime;
  String? name;
  Null? path;
  double? size;
  int? width;
  int? height;

  Small(
      {this.ext,
      this.url,
      this.hash,
      this.mime,
      this.name,
      this.path,
      this.size,
      this.width,
      this.height});

  Small.fromJson(Map<String, dynamic> json) {
    ext = json['ext'];
    url = json['url'];
    hash = json['hash'];
    mime = json['mime'];
    name = json['name'];
    path = json['path'];
    size = json['size'];
    width = json['width'];
    height = json['height'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['ext'] = this.ext;
    data['url'] = this.url;
    data['hash'] = this.hash;
    data['mime'] = this.mime;
    data['name'] = this.name;
    data['path'] = this.path;
    data['size'] = this.size;
    data['width'] = this.width;
    data['height'] = this.height;
    return data;
  }
}

class Meta {
  Pagination? pagination;

  Meta({this.pagination});

  Meta.fromJson(Map<String, dynamic> json) {
    pagination = json['pagination'] != null
        ? new Pagination.fromJson(json['pagination'])
        : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.pagination != null) {
      data['pagination'] = this.pagination!.toJson();
    }
    return data;
  }
}

class Pagination {
  int? page;
  int? pageSize;
  int? pageCount;
  int? total;

  Pagination({this.page, this.pageSize, this.pageCount, this.total});

  Pagination.fromJson(Map<String, dynamic> json) {
    page = json['page'];
    pageSize = json['pageSize'];
    pageCount = json['pageCount'];
    total = json['total'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['page'] = this.page;
    data['pageSize'] = this.pageSize;
    data['pageCount'] = this.pageCount;
    data['total'] = this.total;
    return data;
  }
}


这是使用http的块代码

class CoursesBloc extends Bloc<CoursesEvent, CoursesState> {
  CoursesBloc() : super(CoursesInitial()) {
    on<CoursesInitialEvent>(coursesInitialEvent);
    on<CourseCardNavigateEvent>(courseCardNavigateEvent);
  }
  FutureOr<void> coursesInitialEvent(
      CoursesInitialEvent event, Emitter<CoursesState> emit) async {
    var client = http.Client();

    List<Data> datums = [];
    try{
      var response = await client.get(Uri.parse('http://192.168.66.1:1337/api/courses?populate=CoverPic'));
      List result = jsonDecode(response.body);
      for(var i=0; i<result.length;i++)
        {
          Data datum = Data.fromJson(result[i]);
          datums.add(datum);
        }

      print(datums);
      emit(CoursesLoadedSuccessState(datums));
    }
        catch(e){
      log(e.toString());
        }
  }

}


我得到的错误是:

[log] type '_Map<String, dynamic>' is not a subtype of type 'List<dynamic>'


我觉得这是在Map中调用的Coverpic类,所以我使用了
“http://192.168.66.1:1337/api/courses”,因为它不会填充CoverPic,但我最终得到了相同的错误

g52tjvyc

g52tjvyc1#

你的React体是一张Map。要访问数据列表,需要使用键data

List result = jsonDecode(response.body)['data'];

字符串
你可以检查这个例子。

相关问题