2

我正在尝试从服务器中获取数据,并且我正在使用 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"
        }
      ]
    },

谢谢您的帮助

4

3 回答 3

2

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

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

这就是为什么 null 错误

于 2021-03-26T10:15:24.463 回答
1

该错误很可能发生在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;

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

...
datePublished: (datePublished != null) ? DateTime.parse(datePublished) : null;
...
于 2021-03-26T07:40:14.337 回答
0

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

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

于 2021-03-26T05:01:56.537 回答