2

I'm trying to fetch data from server in flutter and I'm using json_serializable for my data model. I successfully get the data, but when it's time to convert the json in a data list I get this error: Unhandled Exception: type 'Null' is not a subtype of type 'String' in type cast. I don't know how to solve. Here's my fetch function

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);
    }
}

Here's my model

@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);
}

And here's the data I get from server

{
  "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"
        }
      ]
    },

Thanks for the help

Daitarn
  • 109
  • 1
  • 15

3 Answers3

2

Thank you everyone for help, I knew the error was happening somehow in the parsing part of the data, but it wasn't connected to the nullable data (which was handled by the automatic code generate by the package) but to the fact that I forgot to rename keys in the data model e.g.:

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

that's why the null error

Daitarn
  • 109
  • 1
  • 15
1

The error will most likely to be happening with the DateTime fields since it's the only field that show null in the data, and the DateTime.parse() required the value to be not-null. These fields are potentially causing the error:

// 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;

Do a check before parsing them, for example with field datePublished:

...
datePublished: (datePublished != null) ? DateTime.parse(datePublished) : null;
...
Bach
  • 2,928
  • 1
  • 6
  • 16
0

You need to account for the null values you are receiving in date published and date deleted. Do you understand the code that you posted and what the error is trying to explain?

While converting your json map into a class, the error happens there.

Huthaifa Muayyad
  • 11,321
  • 3
  • 17
  • 49