I have a problem. I created the following class:
enum WeatherType {
@JsonValue("sunny")
sunny,
@JsonValue("raining")
raining
}
class Location {
final String locationName;
final String adres;
final num? distance;
final num windSpeed;
final num windDirection;
final String windIconColor;
final num temperature;
final WeatherType weatherType;
TimeOfDay? arrivalTime;
Location(
{required this.locationName,
required this.adres,
required this.windSpeed,
required this.windDirection,
required this.temperature,
required this.weatherType,
required this.windIconColor,
this.distance});
Location.fromJson(Map<String, dynamic> json)
: locationName = json['locationName'],
adres = json['adres'],
distance = json['distance'],
windSpeed = json['windSpeed'],
windDirection = json['windDirection'],
windIconColor = json['windIconColor'],
temperature = json['temperature'],
weatherType = json['weatherType'],
arrivalTime =
TimeOfDay.fromDateTime(DateTime.parse(json['arrivalTime']));
Map<String, dynamic> toJson() {
return {
'locationName': locationName,
'adres': adres,
'distance': distance,
'windSpeed': windSpeed,
'windDirection': windDirection,
'windIconColor': windIconColor,
'temperature': temperature,
'weatherType': weatherType.toString(),
'arrivalTime': arrivalTime.toString(),
};
}
}
When I store this class as JSON string in the localstorage, it gets stored like this:
{"locationName":"Castricum aan Zee","adres":"Strand, 1901 NZ Castricum","distance":4,"windSpeed":12,"windDirection":90,"windIconColor":"76E648","temperature":5,"weatherType":"WeatherType.raining","arrivalTime":"TimeOfDay(23:20)"}
Then when I use the following line of code:
Location test = Location.fromJson(jsonDecode(json));
The app freezes when running that fromJson
method. What am I doing wrong in the encoding/decoding of my class?