【问题标题】:Flutter json_serializable models error: Unhandled Exception: type 'Null' is not a subtype of type 'String' in type castFlutter json_serializable模型错误:未处理的异常:类型'Null'不是类型转换中'String'类型的子类型
【发布时间】:2021-06-22 20:27:54
【问题描述】:

我正在尝试从服务器获取数据,并且我正在使用 json_serializable 作为我的数据模型。 我成功地获取了数据,但是当需要转换数据列表中的 json 时,我收到此错误:未处理的异常:类型“Null”不是类型转换中“字符串”类型的子类型。 我不知道怎么解决。 这是我的获取函数

Future<List<Data>> getallNews() async {
    try {
      var response = await NewsAppApi.dio.get(ALL_NEWS);
      // If the server did return a 200 OK response,
      // then parse the JSON.
      List parsed = response.data['data'];
      List<Data> _news = [];
      parsed.forEach((element) {
        print(element);
        Data n =  Data.fromJson(element);
        print(n);
        _news.add(n);
      });
      //List<Data> _news = parsed.map((json) => Data.fromJson(json)).toList();
      return _news;
    } on DioError catch (e) {
      throw showNetworkError(e);
    }
}

这是我的模型

@JsonSerializable(explicitToJson: true)
class Data {
  final int id;
  final int author;
  final String title;
  final String body;
  final String link;
  final DateTime? datePublished;
  final DateTime dateTobePublished;
  final int published;
  final int tobePublished;
  final int status;
  final DateTime? deletedAt;
  final DateTime createdAt;
  final DateTime updatedAt;
  final List<Tags> tags;

  Data(
      {
      required this.id,
      required this.author,
      required this.title,
      required this.body,
      required this.link,
      required this.datePublished,
      required this.dateTobePublished,
      required this.published,
      required this.tobePublished,
      required this.status,
      required this.deletedAt,
      required this.createdAt,
      required this.updatedAt,
      required this.tags});

  
  factory Data.fromJson(Map<String, dynamic> json) => _$DataFromJson(json);

 
  Map<String, dynamic> toJson() => _$DataToJson(this);
}

这是我从服务器获取的数据

{
  "data": [
    {
      "id": 105,
      "author": 1,
      "title": "testLaura",
      "body": "asdadsdas",
      "link": "https:\/\/www.google.com\/",
      "datePublished": null,
      "dateTobePublished": "2021-03-09 22:51:00",
      "published": 0,
      "tobePublished": 1,
      "status": 0,
      "deleted_at": null,
      "created_at": "2021-03-09T08:18:02.000000Z",
      "updated_at": "2021-03-09T08:18:02.000000Z",
      "tags": [
        {
          "id": 4,
          "title": "Studenti"
        }
      ]
    },
    {
      "id": 104,
      "author": 8,
      "title": "news",
      "body": "sdadasdasasdasddasads",
      "link": "https:\/\/www.google.com\/",
      "datePublished": null,
      "dateTobePublished": "2021-03-09 08:11:20",
      "published": 0,
      "tobePublished": 1,
      "status": 0,
      "deleted_at": null,
      "created_at": "2021-03-09T08:12:36.000000Z",
      "updated_at": "2021-03-09T08:12:36.000000Z",
      "tags": [
        {
          "id": 2,
          "title": "Genitori"
        }
      ]
    },

感谢您的帮助

【问题讨论】:

  • 标签类是如何定义的?
  • 请分享_$DataFromJson(json)的代码

标签: flutter dart json-serializable


【解决方案1】:

谢谢大家的帮助,我知道错误是在解析数据部分发生的,但它没有连接到可空数据(由包生成的自动代码处理),而是连接到我忘记重命名数据模型中的键的事实,例如:

  @JsonKey(name: 'deleted_at')
  final DateTime? deletedAt;

这就是 null 错误的原因

【讨论】:

    【解决方案2】:

    错误最有可能发生在 DateTime 字段中,因为它是数据中唯一显示 null 的字段,而 DateTime.parse() 要求该值不为空。这些字段可能会导致错误:

    // These fields are causing the error
    final DateTime? datePublished;
    final DateTime? deletedAt;
    
    // Other fields that could bring the error as well
    final DateTime dateTobePublished;
    final DateTime createdAt;
    final DateTime updatedAt;
    

    在解析它们之前进行检查,例如使用字段datePublished

    ...
    datePublished: (datePublished != null) ? DateTime.parse(datePublished) : null;
    ...
    

    【讨论】:

      【解决方案3】:

      您需要考虑在发布日期和删除日期收到的空值。您了解您发布的代码以及错误试图解释的内容吗?

      在将您的 json 映射转换为类时,错误发生在那里。

      【讨论】:

        猜你喜欢
        • 2021-09-22
        • 2023-01-27
        • 2022-06-30
        • 2022-01-18
        • 2021-10-11
        • 2021-12-16
        • 1970-01-01
        • 2022-01-13
        • 2021-12-28
        相关资源
        最近更新 更多