1

I'm trying to use the API in my application it works normally, but when JSON gets null the app doesn't work.

works normally:

{
   "product":{
      "description_1":"Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
      "description_2":"Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old.",
      "description_3":"There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteratenter code hereion in some form",

   }
}

Error: "type 'null' is not a subtype of type 'map string dynamic ' ":

{ "product":null }

Class:

class Store {
  Store({
    required this.product,
  });

  Product product;

  factory Store.fromJson(Map<String, dynamic> json) => Store(
        product: Product.fromJson(json["product"]),
      );

  Map<String, dynamic> toJson() => {
        "product": product.toJson(),
      };
}

class Product {
  Product({
    required this.description1,
    required this.description2,
    required this.description3,
  });

  String description1;
  String description2;
  String description3;

  factory Product.fromJson(Map<String, dynamic> json) => Product(
        description1: json["description_1"] ?? "",
        description2: json["description_2"] ?? "",
        description3: json["description_3"] ?? "",
      );

  Map<String, dynamic> toJson() => {
        "description_1": description1,
        "description_2": description2,
        "description_3": description3,
      };
}

1 Answer 1

2

If product can actually be null then Store needs to deal with it. Change the Store class product property to be nullable with ? symbol like the following:

class Store {
  Store({
    this.product,
  });

  Product? product;

  factory Store.fromJson(Map<String, dynamic> json) => Store(
        product: json["product"] != null 
            ? Product.fromJson(json["product"])
            : null,
      );

  Map<String, dynamic> toJson() => {
        "product": product?.toJson(),
      };
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the answer. But the parameters "description_1", "description_2", "description_3", "description_4", "description_5", are not sent and product receives only "null" it still has an error.
Just correct the answer from json["product"] != null to json["product"] is Map.
@Rodriguessilva, is the JSON like this? {"product": null}?
@lepsch Yes, some JSON comes without description_1, description_2, description_3. only NULL
The solution I gave should deal with this situation smoothly. Isn't it enough? What exactly are you looking for if product == null?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.