Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to deal with empty JSON object?

I’m working with an API that returns empty list when the app start. Its showing the following error in the particuler line: productList: List<ProductList>.from(json["ProductList"].map((x) => ProductList.fromJson(x))),

How can I handle this error? I saw some solution they providing an empty list[]. If you just tell me what syntax I should use here to handle this kind of error…

For this error my App loading infinite time sometimes.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

error

My Model class

import 'package:meta/meta.dart';
import 'dart:convert';

MyCartItemListModel myCartItemListModelFromJson(String str) =>
    MyCartItemListModel.fromJson(json.decode(str));

String myCartItemListModelToJson(MyCartItemListModel data) =>
    json.encode(data.toJson());

class MyCartItemListModel {
  MyCartItemListModel({
    required this.status,
    required this.message,
    required this.userId,
    required this.cookieVal,
    required this.totalItem,
    required this.cartProductTotal,
    required this.shippingCharge,
    required this.cartTotal,
    required this.productList,
  });

  int status;
  String message;
  String userId;
  String cookieVal;
  String totalItem;
  String cartProductTotal;
  String shippingCharge;
  String cartTotal;
  List<ProductList> productList;

  factory MyCartItemListModel.fromJson(Map<String, dynamic> json) =>
      MyCartItemListModel(
        status: json["Status"],
        message: json["Message"],
        userId: json["UserId"] ??"",
        cookieVal: json["CookieVal"],
        totalItem: json["TotalItem"],
        cartProductTotal: json["CartProductTotal"],
        shippingCharge: json["ShippingCharge"],
        cartTotal: json["CartTotal"],
        productList: List<ProductList>.from(json["ProductList"].map((x) => ProductList.fromJson(x))),
      );

  Map<String, dynamic> toJson() => {
        "Status": status,
        "Message": message,
        "UserId": userId,
        "CookieVal": cookieVal,
        "TotalItem": totalItem,
        "CartProductTotal": cartProductTotal,
        "ShippingCharge": shippingCharge,
        "CartTotal": cartTotal,
        "ProductList": List<dynamic>.from(productList.map((x) => x.toJson())),
      };
}

class ProductList {
  ProductList({
    required this.cartId,
    required this.productId,
    required this.productName,
    required this.productImage,
    required this.productSize,
    required this.productColor,
    required this.productRate,
    required this.quantity,
    required this.productTotal,
  });

  String cartId;
  String productId;
  String productName;
  String productImage;
  String productSize;
  String productColor;
  String productRate;
  String quantity;
  String productTotal;

  factory ProductList.fromJson(Map<String, dynamic> json) => ProductList(
        cartId: json["CartId"],
        productId: json["ProductId"],
        productName: json["ProductName"],
        productImage: json["ProductImage"],
        productSize: json["ProductSize"],
        productColor: json["ProductColor"],
        productRate: json["ProductRate"],
        quantity: json["Quantity"],
        productTotal: json["ProductTotal"],
      );

  Map<String, dynamic> toJson() => {
        "CartId": cartId,
        "ProductId": productId,
        "ProductName": productName,
        "ProductImage": productImage,
        "ProductSize": productSize,
        "ProductColor": productColor,
        "ProductRate": productRate,
        "Quantity": quantity,
        "ProductTotal": productTotal,
      };
}

My json Response:

   {
    "Status": 1,
    "Message": "",
    "UserId": "2",
    "CookieVal": "",
    "TotalItem": "4",
    "CartProductTotal": "1767",
    "ShippingCharge": "50",
    "CartTotal": "1817",
    "ProductList": [
        {
            "CartId": "450",
            "ProductId": "10622",
            "ProductName": "     Kids Baby Leggings Pink",
            "ProductImage": "https://sleepkart.co.in/productimage/zb9diak47ocm0q957itf_1.jpg",
            "ProductSize": "L",
            "ProductColor": "#fdc291",
            "ProductRate": "190",
            "Quantity": "1",
            "ProductTotal": "190"
        },
        {
            "CartId": "449",
            "ProductId": "10623",
            "ProductName": "Kids Baby Leggings Green",
            "ProductImage": "https://sleepkart.co.in/productimage/ogr137q1kjr9fiqwdipd_1.jpg",
            "ProductSize": "L",
            "ProductColor": "#42d19a",
            "ProductRate": "193",
            "Quantity": "1",
            "ProductTotal": "193"
        },
        {
            "CartId": "438",
            "ProductId": "10661",
            "ProductName": "Night Suit for Women",
            "ProductImage": "https://sleepkart.co.in/productimage/4jcrpnqw655vg7yoyvun_1.jpg",
            "ProductSize": "L",
            "ProductColor": "#f2be02",
            "ProductRate": "975",
            "Quantity": "1",
            "ProductTotal": "975"
        },
        {
            "CartId": "437",
            "ProductId": "10575",
            "ProductName": "Men's Navy Blue Bermuda",
            "ProductImage": "https://sleepkart.co.in/productimage/zn8oqvspajuks9u1pre4_1.jpg",
            "ProductSize": "FREE",
            "ProductColor": "#0c1155",
            "ProductRate": "409",
            "Quantity": "1",
            "ProductTotal": "409"
        }
    ]
}

>Solution :

You can check like this:

      productList: json["ProductList"] == null ? [] :
      List<ProductList>.from(json["ProductList"].map((x)
      =>ProductList.fromJson(x)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading