Pixxu Waku
Pixxu Waku

Reputation: 443

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

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();

Upvotes: 2

Views: 2145

Answers (2)

Pixxu Waku
Pixxu Waku

Reputation: 443

So the first thing was to update the enum property of Model class to String and remove IconData. Then for the second issue. update the decoding function like

      List<SuggestionModel> list =
          addedObj.map((e) => SuggestionModel.fromJson(e)).toList();

Upvotes: 0

Josteve Adekanbi
Josteve Adekanbi

Reputation: 12673

Try this:

String encodedData = jsonEncode(list.map((e) => e.toJson()).toList());

Upvotes: 1

Related Questions