Search code examples
jsonflutterdata-storagejsonserializer

Flutter JsonSerialization


I am currently building an app where the user is able to store List items as favorites on their device. Debugging my app I am running in the following error:

Exception has occurred.
_CastError (type 'Null' is not a subtype of type 'Widget' in type cast)

This is my code:

LogEntry _$LogEntryFromJson(Map<String, dynamic> json) {
  return LogEntry(
      mechanismId: json['mechanismId'] as int?,
      timestamp: DateTime.parse(json['timestamp'] as String),
      navigation: json['navigation'] as Widget);
}

Map<String, dynamic> _$LogEntryToJson(LogEntry instance) => <String, dynamic>{
      'mechanismId': instance.mechanismId,
      'timestamp': instance.timestamp.toIso8601String(),
      'navigation:': instance.navigation,
    };

The mechanismID is the respective ID of a List item and the navigation stands for the route of the detail page.


Solution

  • LogEntry _$LogEntryFromJson(Map<String, dynamic> json) {
      return LogEntry(
          mechanismId: json['mechanismId'] as int?,
          timestamp: DateTime.parse(json['timestamp'] as String),
          navigation: json['navigation'] as Widget?);
    }