Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I pass an instance of a object by value?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading