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

Conversion error from object to json and back in flutter

I am trying to convert a list of objects as a json string in shared preferences.

Object class

SuggestionModel suggestionModelFromJson(String str) =>
    SuggestionModel.fromJson(json.decode(str));

String suggestionModelToJson(SuggestionModel data) =>
    json.encode(data.toJson());

class SuggestionModel {
  SuggestionModel({
    this.category,
    this.icon,
    this.subs,
  });

  eCategory? category;
  IconData? icon;
  List<Sub>? subs;

  factory SuggestionModel.fromJson(Map<String, dynamic> json) =>
      SuggestionModel(
        category: json["category"],
        icon: json["icon"],
        subs: List<Sub>.from(json["subs"].map((x) => Sub.fromJson(x))),
      );

  Map<String, dynamic> toJson() => {
        "category": category,
        "icon": icon,
        "subs": List<dynamic>.from(subs!.map((x) => x.toJson())),
      };
}

class Sub {
  Sub({
    this.subCategory,
    this.values,
  });

  String? subCategory;
  List<Value>? values;

  factory Sub.fromJson(Map<String, dynamic> json) => Sub(
        subCategory: json["sub_category"],
        values: List<Value>.from(json["values"].map((x) => Value.fromJson(x))),
      );

  Map<String, dynamic> toJson() => {
        "sub_category": subCategory,
        "values": List<dynamic>.from(values!.map((x) => x.toJson())),
      };
}

class Value {
  Value({
    this.subName,
    this.selected,
  });

  String? subName;
  bool? selected;

  factory Value.fromJson(Map<String, dynamic> json) => Value(
        subName: json["sub_name"],
        selected: json["selected"],
      );

  Map<String, dynamic> toJson() => {
        "sub_name": subName,
        "selected": selected,
      };
}

When I try to do

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

List<SuggestionModel> list;
String encodedData = jsonEncode(list);

it gives me an error

Converting object to an encodable object failed: Instance of ‘SuggestionModel’

Im not following where the exact issue comes from. tried debugging and still no luck
How can I rectify this?

Update. I’ve changed the enum to a String and removed the IconData field. And the above issue had resolved.

Now when I try to get the saved Json string and convert that back to list of objects. I get an error

Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<SuggestionModel>'

at this line

var t = await _dataManager.getSelectedList(s);
var addedObj = json.decode(json.decode(t!));
      
//here...
var list = addedObj.map((e) => SuggestionModel.fromJson(e)).toList();

>Solution :

Try this:

String encodedData = jsonEncode(list.map((e) => e.toJson()).toList());
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