Unhandled Exception: Converting object to an encodable object failed: _LinkedHashMap

Issue

I am new to flutter , i have been able to successully call api’s however when i modified my model class by adding map<String,dynamic> i am getting the following below exception
Unhandled Exception: Converting object to an encodable object failed: _LinkedHashMap

My model class for the reference is as below

class Profile {
  String id;
  String userId;
  String username;
  String password;
  String email;
  String arabicFullNam;
  String englishFullNam;
  String phoneNumber;
  String civilId;
  int type;
  bool success;
  List<dynamic> errors;
  Map<String,dynamic>body;

  Profile({this.id, this.userId,this.username, this.password, this.arabicFullNam, this.englishFullNam, this.email,this.civilId, this.errors,this.success,this.type,this.body,this.phoneNumber});

  factory Profile.fromJson(Map<String, dynamic> map) {
    return Profile(
        id: map["id"], userId: map["userId"],phoneNumber: map["phoneNumber"],username: map["username"], password: map["password"], englishFullNam: map["fullnameEn"], arabicFullNam: map['fullnameAr'], email: map['email'],civilId:map['civilId'], errors: map['errors'], success: map['success'],type: map['type'],body: map['body']);
  }

  Map<String, dynamic> toJson() {
    return {"id": id, "userId": userId,"username": username, phoneNumber: "phoneNumber", "password": password, "fullnameEn": englishFullNam, "fullnameAr": arabicFullNam,"email": email, "civilId": civilId,"success": success, "type": type, "body": body};
  }

  @override
  String toString() {
    return 'Profile{id: $id,userId: $userId, username: $username, password: $password, email: $email, fullnameEn: $englishFullNam, fullnameAr: $arabicFullNam, civilId: $civilId, errors: $errors, success: $success, type: $type, body: $body, phoneNumber: $phoneNumber }';
  }


}

List<Profile> profileFromJson(String jsonData) {
  final data = json.decode(jsonData);
  return List<Profile>.from(data.map((item) => Profile.fromJson(item)));
}


Profile postFromJson(String str) {
  final jsonData = json.decode(str);
  return Profile.fromJson(jsonData);
}


String profileToJson(Profile data) {
  final jsonData = data.toJson();
  print("##--tojson"+jsonData.toString());
  return json.encode(jsonData);
}

Any help would be much appreciated

Solution

Try this

class Profile {
  String id;
  String userId;
  String username;
  String password;
  String email;
  String arabicFullNam;
  String englishFullNam;
  String phoneNumber;
  String civilId;
  int type;
  bool success;
  List<dynamic> errors;
  Map<String, dynamic> body;
  Profile({
    this.id,
    this.userId,
    this.username,
    this.password,
    this.email,
    this.arabicFullNam,
    this.englishFullNam,
    this.phoneNumber,
    this.civilId,
    this.type,
    this.success,
    this.errors,
    this.body,
  });

  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'userId': userId,
      'username': username,
      'password': password,
      'email': email,
      'arabicFullNam': arabicFullNam,
      'englishFullNam': englishFullNam,
      'phoneNumber': phoneNumber,
      'civilId': civilId,
      'type': type,
      'success': success,
      'errors': errors,
      'body': body,
    };
  }

  factory Profile.fromMap(Map<String, dynamic> map) {
    if (map == null) return null;

    return Profile(
      id: map['id'],
      userId: map['userId'],
      username: map['username'],
      password: map['password'],
      email: map['email'],
      arabicFullNam: map['arabicFullNam'],
      englishFullNam: map['englishFullNam'],
      phoneNumber: map['phoneNumber'],
      civilId: map['civilId'],
      type: map['type'],
      success: map['success'],
      errors: List<dynamic>.from(map['errors']),
      body: Map<String, dynamic>.from(map['body']),
    );
  }

  String toJson() => json.encode(toMap());

  factory Profile.fromJson(String source) =>
      Profile.fromMap(json.decode(source));

  @override
  String toString() {
    return 'Profile(id: $id, userId: $userId, username: $username, password: $password, email: $email, arabicFullNam: $arabicFullNam, englishFullNam: $englishFullNam, phoneNumber: $phoneNumber, civilId: $civilId, type: $type, success: $success, errors: $errors, body: $body)';
  }
}

Answered By – Raine Dale Holgado

Answer Checked By – Senaida (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *