flutter error : Expected a value of type 'int' but got one of type 'Null'

312

The error ocurrs in your "fromJson" parsing, replace your factory fromJson function to this:

factory Establishment.fromJson(Map<String, dynamic> json) {
        return Establishment(
            id: json['establishmentId'],
            name: json['establishmentName'],
            addressid: json['addressId'],
            documentationid: json['documentationId']);
      }

Tip: Check the camelCase name, the names must be equals in your parsing.

Share:
312
GIBS
Author by

GIBS

Updated on January 03, 2023

Comments

  • GIBS
    GIBS over 1 year

    response body from the .NET Core 6 API :

    [{"establishmentId":1,"establishmentName":"Secret","addressId":1,"documentationId":1,"address":null,"documentation":null,"associationsEts":[],"prices":[]},{"establishmentId":2,"establishmentName":"HRB","addressId":2,"documentationId":2,"address":null,"documentation":null,"associationsEts":[],"prices":[]}]
    

    My model class :

        class Establishment {
          final int id;
          final String name;
          final int addressid;
          final int documentationid;
        
          Establishment(
              {required this.id,
              required this.name,
              required this.addressid,
              required this.documentationid});
        
          factory Establishment.fromJson(Map<String, dynamic> json) {
            return Establishment(
                id: json['id'],
                name: json['name'],
                addressid: json['addressid'],
                documentationid: json['documantationid']);
          }
        }
    

    The problem is that snapshot got an error , I would like snapshot to accept null values, could someone help me to fix this ? Thanks

    UPDATE :

    The problem doesn't occurs in the from json beacuase since I modified them, the probelm still occurs

    class Establishment {
      final int establishmentId;
      final String establishmentName;
      final int addressId;
      final int documentationId;
    
      Establishment(
          {required this.establishmentId,
          required this.establishmentName,
          required this.addressId,
          required this.documentationId});
    
      factory Establishment.fromJson(Map<String, dynamic> json) {
        return Establishment(
            establishmentId: json['establishmentId'],
            establishmentName: json['establishmentName'],
            addressId: json['addressId'],
            documentationId: json['documantationId']);
      }
    }
    
    • John Siu
      John Siu almost 2 years
      Same issue, do you have a solution? BTW, if an answer doesn't solve the issue, you can remove the checkmark.
  • GIBS
    GIBS over 2 years
    thanks ! have done it but stil get snapshot error
  • GIBS
    GIBS over 2 years
    what could I do ?