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