Say one has an abstract
Car
class with a derived Cabrio
class.
From a REST api he recieves a JSON with data
abstract class Car {
int id;
String name;
String description;
Car({
this.id,
this.name,
this.description,
});
factory Car.fromJson(Map<String, dynamic> json, String type) {
Car car;
if (type == 'cabrio') car = Cabrio.fromJson(json);
// other possible if-statements
car.id = int.parse(json['id']);
car.name = json['name'];
car.description = json['description'];
return car;
}
class Cabrio extends Car {
String roofMaterial;
String someOtherProp;
Cabrio({
id,
name,
this.roofMaterial,
this.someOtherProp
}) : super(
id: id,
name: name,
description: description);
factory Cabrio.fromJson(Map<String, dynamic> json) =>
Cabrio(
roofMaterial: json['roof_material'],
someOtherProp: json['some_other_prop']
);
}
dynamic dyn = jsonDecode(response.body);
Cabrio cabrio = Car.fromJson(dyn[0], 'cabrio');
cabrio.roofMaterial // null
cabrio.someOtherProp // null
return cabrio;
Why is cabrio.roofMaterial
or cabrio.someOtherProp
null
?
Why I am taking this approach
I didn't like seeing for example
id: json['id']
in all derived classes. This approach is to prevent such redundancy
What I know
- according to the debugger, the properties of the derived class
Cabrio
are set correctly by the json values in it'sfromJson
- when inspecting the
car
object atcar.name = json['name']
the derived class' properties (likecabrio.roofMaterial
) are alreadynull
What I consider to be a problem at
if (type == 'cabrio') car = Cabrio.fromJson(json, type);
I am 'pushing' a cabrio
object into a Car
object (which has less properties than Cabrio
). But that should not be wrong since it's just a parent class type