Home > database >  How to resolve: type 'Null' is not a subtype of type 'Map<String, dynamic>'
How to resolve: type 'Null' is not a subtype of type 'Map<String, dynamic>'

Time:07-26

In flutter, I have an api thats giving me this list of json objects. This is one of the object :

{
"_id": "62dd4989eef1e8863eb65ffe",
"shoopkeper": {
"_id": "62d78910a0d65589c3d112af",
"userName": "lakshya",
"location": {
"lat": 23.2403327,
"lng": 77.4389773
}
},
"offerId": {
 "_id": "62da41208be0d1f7a092e4ea",
"offername": "thisisoffername",
"imageURL": ""
},
"userID": {
"_id": "62d5038dd7d46f3c025a0d3f",
"userName": "Mahak "
},
"state": 0,
"createdAt": "2022-07-24T13:30:49.534Z",
     "updatedAt": "2022-07-24T13:30:49.534Z",
     "__v": 0
   },

This is my json parsing I have done :



class CollectedOffer {
  CollectedOffer({
    this.id,
    this.shoopkeper,
    this.offerId,
    this.userId,
    this.state,
    this.createdAt,
    this.updatedAt,
    this.v,
    this.redeemDate,
  });

  String? id;
  Shoopkeper? shoopkeper;
  OfferId? offerId;
  UserId? userId;
  int? state;
  DateTime? createdAt;
  DateTime? updatedAt;
  int? v;
  DateTime? redeemDate;

  factory CollectedOffer.fromJson(Map<String, dynamic> json) => CollectedOffer(
        id: json["_id"],
        shoopkeper: Shoopkeper.fromJson(json["shoopkeper"]),
        offerId: OfferId.fromJson(json["offerId"]),
        userId: UserId.fromJson(json["userID"]),
        state: json["state"],
        createdAt: DateTime.parse(json["createdAt"]),
        updatedAt: DateTime.parse(json["updatedAt"]),
        v: json["__v"],
        redeemDate: DateTime.parse(json["redeemDate"]),
      );
}

class OfferId {
  OfferId({
    this.id,
    this.offername,
    this.imageUrl,
  });

  String? id;
  String? offername;
  String? imageUrl;

  factory OfferId.fromJson(Map<String, dynamic> json) => OfferId(
        id: json["_id"],
        offername: json["offername"],
        imageUrl: json["imageURL"],
      );
}

class Shoopkeper {
  Shoopkeper({
    this.id,
    this.userName,
    this.location,
  });

  String? id;
  String? userName;
  Location? location;

  factory Shoopkeper.fromJson(Map<String, dynamic> json) => Shoopkeper(
        id: json["_id"],
        userName: json["userName"],
        location: Location.fromJson(json["location"]),
      );
}

class Location {
  Location({
    this.lat,
    this.lng,
  });

  double? lat;
  double? lng;

  factory Location.fromJson(Map<String, dynamic> json) => Location(
        lat: json["lat"],
        lng: json["lng"],
      );
}

class UserId {
  UserId({
    this.id,
    this.userName,
  });

  String? id;
  String? userName;

  factory UserId.fromJson(Map<String, dynamic> json) => UserId(
        id: json["_id"],
        userName: json["userName"],
      );
}

When I use information from above in Future Builder I get this error.

type 'Null' is not a subtype of Map<String dynamic>

Any idea how to solve this? Also : This is just 1 object. some other json objects have userID : null coming. IS that the whats causing issue?

This is the Future builder :

    if (response.statusCode == 200) {
      jsonResponse = json.decode(response.body)['objects'];
      if (jsonResponse.isNotEmpty) {
        setState(() {
          isdataNull = false;
        });
      }
  

      return jsonResponse.map((data) => CollectedOffer.fromJson(data)).toList();
    } else {
      throw Exception('${response.statusCode}');
    }

CodePudding user response:

Probaly the variable json on "CollectedOffer.fromJson" method was null. Check what data your have before send to fromJson method, or pass a empy object, something like that:

CollectedOffer.fromJson(json ?? {})

CodePudding user response:

try this class

class CollectedOffer {
      String? sId;
      Shoopkeper? shoopkeper;
      OfferId? offerId;
      UserID? userID;
      int? state;
      String? createdAt;
      String? updatedAt;
      int? iV;
    
      CollectedOffer(
          {this.sId,
          this.shoopkeper,
          this.offerId,
          this.userID,
          this.state,
          this.createdAt,
          this.updatedAt,
          this.iV});
    
      CollectedOffer.fromJson(Map<String, dynamic> json) {
        sId = json['_id'];
        shoopkeper = json['shoopkeper'] != null
            ? new Shoopkeper.fromJson(json['shoopkeper'])
            : null;
        offerId =
            json['offerId'] != null ? new OfferId.fromJson(json['offerId']) : " ";
        userID =
            json['userID'] != null ? new UserID.fromJson(json['userID']) : " ";
        state = json['state'];
        createdAt = json['createdAt'];
        updatedAt = json['updatedAt'];
        iV = json['__v'];
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['_id'] = this.sId;
        if (this.shoopkeper != null) {
          data['shoopkeper'] = this.shoopkeper!.toJson();
        }
        if (this.offerId != null) {
          data['offerId'] = this.offerId!.toJson();
        }
        if (this.userID != null) {
          data['userID'] = this.userID!.toJson();
        }
        data['state'] = this.state;
        data['createdAt'] = this.createdAt;
        data['updatedAt'] = this.updatedAt;
        data['__v'] = this.iV;
        return data;
      }
    }
    
    class Shoopkeper {
      String? sId;
      String? userName;
      Location? location;
    
      Shoopkeper({this.sId, this.userName, this.location});
    
      Shoopkeper.fromJson(Map<String, dynamic> json) {
        sId = json['_id'];
        userName = json['userName'];
        location = json['location'] != null
            ? new Location.fromJson(json['location'])
            : null;
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['_id'] = this.sId;
        data['userName'] = this.userName;
        if (this.location != null) {
          data['location'] = this.location!.toJson();
        }
        return data;
      }
    }
    
    class Location {
      double? lat;
      double? lng;
    
      Location({this.lat, this.lng});
    
      Location.fromJson(Map<String, dynamic> json) {
        lat = json['lat'];
        lng = json['lng'];
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['lat'] = this.lat;
        data['lng'] = this.lng;
        return data;
      }
    }
    
    class OfferId {
      String? sId;
      String? offername;
      String? imageURL;
    
      OfferId({this.sId, this.offername, this.imageURL});
    
      OfferId.fromJson(Map<String, dynamic> json) {
        sId = json['_id'];
        offername = json['offername'];
        imageURL = json['imageURL'];
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['_id'] = this.sId;
        data['offername'] = this.offername;
        data['imageURL'] = this.imageURL;
        return data;
      }
    }
    
    class UserID {
      String? sId;
      String? userName;
    
      UserID({this.sId, this.userName});
    
      UserID.fromJson(Map<String, dynamic> json) {
        sId = json['_id'];
        userName = json['userName'];
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['_id'] = this.sId;
        data['userName'] = this.userName;
        return data;
      }
    }
  • Related