Home > Software engineering >  Unhandled Exception: type 'Null' is not a subtype of type 'int' in type cast
Unhandled Exception: type 'Null' is not a subtype of type 'int' in type cast

Time:07-23

factory Account.fromJson(Map<String, dynamic> json) {
    return Account(
        json["id"],
        json["email"],
        json["description"],
        json["first_name"],
        json["last_name"],
        json["phone"],
        json["username"],
        json["image"],
        json["email_confirmed"],
        json["reviews_media"] != null
            ? double.parse(json["reviews_media"])
            : (json["reviews_media"] as int).toDouble(),
        json["holiday_mode"],
        json["identity_code"],
        json["residency_city"],
        json["birthday"] != null
            ? DateFormat("yyyy-MM-dd").parse(json["birthday"])
            : null);
  }

CodePudding user response:

Solution: Replacing int with int? (making fields optional will resolve issue).

CodePudding user response:

The problem is here :

json["reviews_media"] != null
            ? double.parse(json["reviews_media"])
            : (json["reviews_media"] as int).toDouble(),

If json["reviews_media"] is NOT null you parse it as double which is file, but the other part of the condition is wrong. You know that the right part is null you can't cast it to int. You should default the null condition to something like this :

json["reviews_media"] != null
            ? double.parse(json["reviews_media"])
            : 0.0,
  • Related