I have two model class. One class I have defined PreferenceModel as a property. PreferenceModel contains one property called showData which is a bool.
Now my json is printing:
preference: Instance of 'PreferenceModel'
I want it to print
preference: { showData:true }
>Solution :
You have create a method toJson in your parent and child classes to convert the class object to map object. For example your class PreferenceModel should be defined as below with toJson() method.
class PreferenceModel {
bool? showData;
PreferenceModel({this.showData});
PreferenceModel.fromJson(Map<String, dynamic> json) {
showData = json['showData'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['showData'] = this.showData;
return data;
}
}