flutter 无此类方法错误:对null调用了方法"map

ffvjumwh  于 2023-01-27  发布在  Flutter
关注(0)|答案(3)|浏览(121)

[错误:flutter/lib/ui/ui_dart_state. cc(148)]未处理的异常:无此类方法错误:在null. E/flutter(19718)上调用了方法"map":接收器:零E/ Flutter (19718):尝试呼叫:Map(闭合:(动态)=〉序列号)
我尝试了json_annotation和json可序列化,但是不起作用。根据我的模型,一个json是可以的。但是两个json是需要错误发生的标题。如何解决。我知道系列号是错误的,但是我不知道如何解决。
这是一个. json

{
   "bookDetail": {
    "title": "aaa",
    "author": "aaa",
    "image": "https://",
    "text": "aaa",
    "series_no": [
        {
            "id": 2
        }
    ],
    "created_at": "2019-08-27 15:19:10"
  }
}

这是两个. json

{
"bookDetail": {
    "title": "Test",
    "author": "TEst",
    "image": "https:/riv19q9x.png",
    "text": "Test",
    "series_no": null,
    "created_at": "2019-08-27 15:13:56"
  }
}

这是使用块Flutter的详细模型

class BookDetailModel {
    BookDetail bookDetail;

    BookDetailModel({
    this.bookDetail,
 });

factory BookDetailModel.fromJson(Map<String, dynamic> json) =>
  new BookDetailModel(
    bookDetail: BookDetail.fromJson(json["bookDetail"]),
   );

Map<String, dynamic> toJson() => {
    "bookDetail": bookDetail.toJson(),
  };
}

  @JsonSerializable(nullable: true)
   class BookDetail {
   String title;
   String author;
   String image;
   String text;

   List<SeriesNo> seriesNo;
   DateTime createdAt;

   BookDetail({
    this.title,
    this.author,
   this.image,
   this.text,
   this.seriesNo,
  this.createdAt,
 });

factory BookDetail.fromJson(Map<String, dynamic> json) => new BookDetail(
    title: json["title"],
    author: json["author"],
    image: json["image"],
    text: json["text"],
    seriesNo: new List<SeriesNo>.from(
        json["series_no"].map((x) => SeriesNo.fromJson(x))),
    createdAt: DateTime.parse(json["created_at"]),
  );

  Map<String, dynamic> toJson() => {
    "title": title,
    "author": author,
    "image": image,
    "text": text,
    "series_no": new List<dynamic>.from(seriesNo.map((x) => x.toJson())),
    "created_at": createdAt.toIso8601String(),
  };
 }

  @JsonSerializable(nullable: true)
  class SeriesNo {
   int id;

   SeriesNo({
     this.id,
  });

  factory SeriesNo.fromJson(Map<String, dynamic> json) => new SeriesNo(
    id: json["id"],
  );

  Map<String, dynamic> toJson() => {
    "id": id,
  };
}
slmsl1lt

slmsl1lt1#

尝试在以下时间之前验证是否不为空:

seriesNo: json["series_no"] != null ? new List<SeriesNo>.from( json["series_no"].map((x) => SeriesNo.fromJson(x))) : List<SeriesNo>().

这就是我问题的答案。

anauzrmj

anauzrmj2#

尝试将seriesNo设置为动态,并将其余字段

class BookDetailModel {
    BookDetail bookDetail;

    BookDetailModel({
        this.bookDetail,
    });

    factory BookDetailModel.fromJson(Map<String, dynamic> json) => BookDetailModel(
        bookDetail: BookDetail.fromJson(json["bookDetail"]),
    );

    Map<String, dynamic> toJson() => {
        "bookDetail": bookDetail.toJson(),
    };
}

class BookDetail {
    String title;
    String author;
    String image;
    String text;
    dynamic seriesNo;
    DateTime createdAt;

    BookDetail({
        this.title,
        this.author,
        this.image,
        this.text,
        this.seriesNo,
        this.createdAt,
    });

    factory BookDetail.fromJson(Map<String, dynamic> json) => BookDetail(
        title: json["title"],
        author: json["author"],
        image: json["image"],
        text: json["text"],
        seriesNo: json["series_no"],
        createdAt: DateTime.parse(json["created_at"]),
    );

    Map<String, dynamic> toJson() => {
        "title": title,
        "author": author,
        "image": image,
        "text": text,
        "series_no": seriesNo,
        "created_at": createdAt.toIso8601String(),
    };
}
uqxowvwt

uqxowvwt3#

如果它是一个List和一个double值,即使它是正确的,又怎么写呢?我遇到了这个问题。请看我的链接。LinkLinkclass

Food _$FoodFromJson(Map<String, dynamic> json) => Food(
      ingredient: (json['ingredient'] as List<dynamic>)
          .map((e) => e as String)
          .toList(),
      quantity:
          (json['quantity'] as List<dynamic>).map((e) => e as String).toList(),
      uom: (json['uom'] as List<dynamic>).map((e) => e as String).toList(),
      step: (json['step'] as List<dynamic>).map((e) => e as String).toList(),
    );

相关问题