Search code examples
flutterapiflutter-http

Null check operator used on a null value while parsing json api in flutter


Following these two api are almost same but one has variant and attributes but other one don't. So I created api model for api 1. and I try to parse both. but only 1st api works and 2nd api returns error. and the error is null check operator used on a null value. Can anyone help me??

  1. https://www.moharaj.com.bd/_public/product/id/wise/7486

  2. https://www.moharaj.com.bd/_public/product/id/wise/7492

this is my api model:

// To parse this JSON data, do

//     final newSingleProductModel = newSingleProductModelFromJson(jsonString);

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

NewSingleProductModel newSingleProductModelFromJson(String str) =>
    NewSingleProductModel.fromJson(json.decode(str));

String newSingleProductModelToJson(NewSingleProductModel data) =>
    json.encode(data.toJson());

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

  Product product;

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

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

class Product {
  Product({
    required this.id,
    required this.name,
    required this.merchantId,
    required this.categoryId,
    required this.subCategoryId,
    required this.brandId,
    required this.subSubCategoryId,
    required this.productCode,
    required this.shopSku,
    required this.slug,
    required this.stock,
    required this.salePrice,
    required this.discount,
    required this.price,
    required this.alertQuantity,
    required this.purchasePrice,
    required this.status,
    required this.productPlacement,
    required this.productPosition,
    required this.campaignId,
    required this.details,
    required this.thumnail,
    required this.discountType,
    required this.createdAt,
    required this.updatedAt,
    required this.productImage,
    required this.productAttribute,
    required this.productVariant,
  });

  int id;
  String name;
  int merchantId;
  int categoryId;
  int subCategoryId;
  dynamic brandId;
  dynamic subSubCategoryId;
  int productCode;
  dynamic shopSku;
  String slug;
  int stock;
  String salePrice;
  int discount;
  String price;
  int alertQuantity;
  String purchasePrice;
  String status;
  int productPlacement;
  int productPosition;
  dynamic campaignId;
  String details;
  String thumnail;
  String discountType;
  DateTime createdAt;
  DateTime updatedAt;
  List<ProductImage> productImage;
  ProductAttribute productAttribute;
  List<ProductVariant> productVariant;

  factory Product.fromJson(Map<String, dynamic> json) => Product(
        id: json["id"],
        name: json["name"],
        merchantId: json["merchant_id"],
        categoryId: json["category_id"],
        subCategoryId: json["sub_category_id"],
        brandId: json["brand_id"],
        subSubCategoryId: json["sub_sub_category_id"],
        productCode: json["product_code"],
        shopSku: json["shop_sku"],
        slug: json["slug"],
        stock: json["stock"],
        salePrice: json["sale_price"],
        discount: json["discount"],
        price: json["price"],
        alertQuantity: json["alert_quantity"],
        purchasePrice: json["purchase_price"],
        status: json["status"],
        productPlacement: json["product_placement"],
        productPosition: json["product_position"],
        campaignId: json["campaign_id"],
        details: json["details"],
        thumnail: json["thumnail"],
        discountType: json["discount_type"],
        createdAt: DateTime.parse(json["created_at"]),
        updatedAt: DateTime.parse(json["updated_at"]),
        productImage: List<ProductImage>.from(
            json["product_image"].map((x) => ProductImage.fromJson(x))),
        productAttribute: ProductAttribute.fromJson(json["product_attribute"]),
        productVariant: List<ProductVariant>.from(
            json["product_variant"].map((x) => ProductVariant.fromJson(x))),
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "name": name,
        "merchant_id": merchantId,
        "category_id": categoryId,
        "sub_category_id": subCategoryId,
        "brand_id": brandId,
        "sub_sub_category_id": subSubCategoryId,
        "product_code": productCode,
        "shop_sku": shopSku,
        "slug": slug,
        "stock": stock,
        "sale_price": salePrice,
        "discount": discount,
        "price": price,
        "alert_quantity": alertQuantity,
        "purchase_price": purchasePrice,
        "status": status,
        "product_placement": productPlacement,
        "product_position": productPosition,
        "campaign_id": campaignId,
        "details": details,
        "thumnail": thumnail,
        "discount_type": discountType,
        "created_at": createdAt.toIso8601String(),
        "updated_at": updatedAt.toIso8601String(),
        "product_image":
            List<dynamic>.from(productImage.map((x) => x.toJson())),
        "product_attribute": productAttribute.toJson(),
        "product_variant":
            List<dynamic>.from(productVariant.map((x) => x.toJson())),
      };
}

class ProductAttribute {
  ProductAttribute({
    required this.id,
    required this.productId,
    required this.attributeId,
    required this.createdAt,
    required this.updatedAt,
    required this.attribute,
  });

  int id;
  int productId;
  int attributeId;
  DateTime createdAt;
  DateTime updatedAt;
  Attribute attribute;

  factory ProductAttribute.fromJson(Map<String, dynamic> json) =>
      ProductAttribute(
        id: json["id"],
        productId: json["product_id"],
        attributeId: json["attribute_id"],
        createdAt: DateTime.parse(json["created_at"]),
        updatedAt: DateTime.parse(json["updated_at"]),
        attribute: Attribute.fromJson(json["attribute"]),
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "product_id": productId,
        "attribute_id": attributeId,
        "created_at": createdAt.toIso8601String(),
        "updated_at": updatedAt.toIso8601String(),
        "attribute": attribute.toJson(),
      };
}

class Attribute {
  Attribute({
    required this.id,
    required this.name,
    required this.status,
    required this.createdAt,
    required this.updatedAt,
    required this.attributeId,
  });

  int id;
  String name;
  int status;
  DateTime createdAt;
  DateTime updatedAt;
  int? attributeId;

  factory Attribute.fromJson(Map<String, dynamic> json) => Attribute(
        id: json["id"],
        name: json["name"],
        status: json["status"],
        createdAt: DateTime.parse(json["created_at"]),
        updatedAt: DateTime.parse(json["updated_at"]),
        attributeId: json["attribute_id"] == null ? null : json["attribute_id"],
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "name": name,
        "status": status,
        "created_at": createdAt.toIso8601String(),
        "updated_at": updatedAt.toIso8601String(),
        "attribute_id": attributeId == null ? null : attributeId,
      };
}

class ProductImage {
  ProductImage({
    required this.id,
    required this.productId,
    required this.productImage,
    required this.createdAt,
    required this.prefixUrl,
    required this.updatedAt,
  });

  int id;
  int productId;
  String productImage;
  DateTime createdAt;
  String prefixUrl;
  DateTime updatedAt;

  factory ProductImage.fromJson(Map<String, dynamic> json) => ProductImage(
        id: json["id"],
        productId: json["product_id"],
        productImage: json["product_image"],
        createdAt: DateTime.parse(json["created_at"]),
        prefixUrl: json["prefix_url"],
        updatedAt: DateTime.parse(json["updated_at"]),
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "product_id": productId,
        "product_image": productImage,
        "created_at": createdAt.toIso8601String(),
        "prefix_url": prefixUrl,
        "updated_at": updatedAt.toIso8601String(),
      };
}

class ProductVariant {
  ProductVariant({
    required this.id,
    required this.productId,
    required this.variantId,
    required this.createdAt,
    required this.updatedAt,
    required this.variant,
  });

  int id;
  int productId;
  int variantId;
  DateTime createdAt;
  DateTime updatedAt;
  Attribute variant;

  factory ProductVariant.fromJson(Map<String, dynamic> json) => ProductVariant(
        id: json["id"],
        productId: json["product_id"],
        variantId: json["variant_id"],
        createdAt: DateTime.parse(json["created_at"]),
        updatedAt: DateTime.parse(json["updated_at"]),
        variant: Attribute.fromJson(json["variant"]),
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "product_id": productId,
        "variant_id": variantId,
        "created_at": createdAt.toIso8601String(),
        "updated_at": updatedAt.toIso8601String(),
        "variant": variant.toJson(),
      };
}

Solution

  • Try this:

    class Model {
      Product? product;
    
      Model({this.product});
    
      Model.fromJson(Map<String, dynamic> json) {
        product =
            json['product'] != null ? new Product.fromJson(json['product']) : null;
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        if (this.product != null) {
          data['product'] = this.product!.toJson();
        }
        return data;
      }
    }
    
    class Product {
      int? id;
      String? name;
      int? merchantId;
      int? categoryId;
      int? subCategoryId;
      Null? brandId;
      Null? subSubCategoryId;
      int? productCode;
      Null? shopSku;
      String? slug;
      int? stock;
      String? salePrice;
      int? discount;
      String? price;
      int? alertQuantity;
      String? purchasePrice;
      String? status;
      int? productPlacement;
      int? productPosition;
      Null? campaignId;
      String? details;
      String? thumnail;
      String? discountType;
      String? createdAt;
      String? updatedAt;
      List<ProductImage>? productImage;
      ProductAttribute? productAttribute;
      List<ProductVariant>? productVariant;
    
      Product(
          {this.id,
          this.name,
          this.merchantId,
          this.categoryId,
          this.subCategoryId,
          this.brandId,
          this.subSubCategoryId,
          this.productCode,
          this.shopSku,
          this.slug,
          this.stock,
          this.salePrice,
          this.discount,
          this.price,
          this.alertQuantity,
          this.purchasePrice,
          this.status,
          this.productPlacement,
          this.productPosition,
          this.campaignId,
          this.details,
          this.thumnail,
          this.discountType,
          this.createdAt,
          this.updatedAt,
          this.productImage,
          this.productAttribute,
          this.productVariant});
    
      Product.fromJson(Map<String, dynamic> json) {
        id = json['id'];
        name = json['name'];
        merchantId = json['merchant_id'];
        categoryId = json['category_id'];
        subCategoryId = json['sub_category_id'];
        brandId = json['brand_id'];
        subSubCategoryId = json['sub_sub_category_id'];
        productCode = json['product_code'];
        shopSku = json['shop_sku'];
        slug = json['slug'];
        stock = json['stock'];
        salePrice = json['sale_price'];
        discount = json['discount'];
        price = json['price'];
        alertQuantity = json['alert_quantity'];
        purchasePrice = json['purchase_price'];
        status = json['status'];
        productPlacement = json['product_placement'];
        productPosition = json['product_position'];
        campaignId = json['campaign_id'];
        details = json['details'];
        thumnail = json['thumnail'];
        discountType = json['discount_type'];
        createdAt = json['created_at'];
        updatedAt = json['updated_at'];
        if (json['product_image'] != null) {
          productImage = <ProductImage>[];
          json['product_image'].forEach((v) {
            productImage!.add(new ProductImage.fromJson(v));
          });
        }
        productAttribute = json['product_attribute'] != null
            ? new ProductAttribute.fromJson(json['product_attribute'])
            : null;
        if (json['product_variant'] != null) {
          productVariant = <ProductVariant>[];
          json['product_variant'].forEach((v) {
            productVariant!.add(new ProductVariant.fromJson(v));
          });
        }
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['id'] = this.id;
        data['name'] = this.name;
        data['merchant_id'] = this.merchantId;
        data['category_id'] = this.categoryId;
        data['sub_category_id'] = this.subCategoryId;
        data['brand_id'] = this.brandId;
        data['sub_sub_category_id'] = this.subSubCategoryId;
        data['product_code'] = this.productCode;
        data['shop_sku'] = this.shopSku;
        data['slug'] = this.slug;
        data['stock'] = this.stock;
        data['sale_price'] = this.salePrice;
        data['discount'] = this.discount;
        data['price'] = this.price;
        data['alert_quantity'] = this.alertQuantity;
        data['purchase_price'] = this.purchasePrice;
        data['status'] = this.status;
        data['product_placement'] = this.productPlacement;
        data['product_position'] = this.productPosition;
        data['campaign_id'] = this.campaignId;
        data['details'] = this.details;
        data['thumnail'] = this.thumnail;
        data['discount_type'] = this.discountType;
        data['created_at'] = this.createdAt;
        data['updated_at'] = this.updatedAt;
        if (this.productImage != null) {
          data['product_image'] =
              this.productImage!.map((v) => v.toJson()).toList();
        }
        if (this.productAttribute != null) {
          data['product_attribute'] = this.productAttribute!.toJson();
        }
        if (this.productVariant != null) {
          data['product_variant'] =
              this.productVariant!.map((v) => v.toJson()).toList();
        }
        return data;
      }
    }
    
    class ProductImage {
      int? id;
      int? productId;
      String? productImage;
      String? createdAt;
      String? prefixUrl;
      String? updatedAt;
    
      ProductImage(
          {this.id,
          this.productId,
          this.productImage,
          this.createdAt,
          this.prefixUrl,
          this.updatedAt});
    
      ProductImage.fromJson(Map<String, dynamic> json) {
        id = json['id'];
        productId = json['product_id'];
        productImage = json['product_image'];
        createdAt = json['created_at'];
        prefixUrl = json['prefix_url'];
        updatedAt = json['updated_at'];
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['id'] = this.id;
        data['product_id'] = this.productId;
        data['product_image'] = this.productImage;
        data['created_at'] = this.createdAt;
        data['prefix_url'] = this.prefixUrl;
        data['updated_at'] = this.updatedAt;
        return data;
      }
    }
    
    class ProductAttribute {
      int? id;
      int? productId;
      int? attributeId;
      String? createdAt;
      String? updatedAt;
      Attribute? attribute;
    
      ProductAttribute(
          {this.id,
          this.productId,
          this.attributeId,
          this.createdAt,
          this.updatedAt,
          this.attribute});
    
      ProductAttribute.fromJson(Map<String, dynamic> json) {
        id = json['id'];
        productId = json['product_id'];
        attributeId = json['attribute_id'];
        createdAt = json['created_at'];
        updatedAt = json['updated_at'];
        attribute = json['attribute'] != null
            ? new Attribute.fromJson(json['attribute'])
            : null;
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['id'] = this.id;
        data['product_id'] = this.productId;
        data['attribute_id'] = this.attributeId;
        data['created_at'] = this.createdAt;
        data['updated_at'] = this.updatedAt;
        if (this.attribute != null) {
          data['attribute'] = this.attribute!.toJson();
        }
        return data;
      }
    }
    
    class Attribute {
      int? id;
      String? name;
      int? status;
      String? createdAt;
      String? updatedAt;
    
      Attribute({this.id, this.name, this.status, this.createdAt, this.updatedAt});
    
      Attribute.fromJson(Map<String, dynamic> json) {
        id = json['id'];
        name = json['name'];
        status = json['status'];
        createdAt = json['created_at'];
        updatedAt = json['updated_at'];
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['id'] = this.id;
        data['name'] = this.name;
        data['status'] = this.status;
        data['created_at'] = this.createdAt;
        data['updated_at'] = this.updatedAt;
        return data;
      }
    }
    
    class ProductVariant {
      int? id;
      int? productId;
      int? variantId;
      String? createdAt;
      String? updatedAt;
      Variant? variant;
    
      ProductVariant(
          {this.id,
          this.productId,
          this.variantId,
          this.createdAt,
          this.updatedAt,
          this.variant});
    
      ProductVariant.fromJson(Map<String, dynamic> json) {
        id = json['id'];
        productId = json['product_id'];
        variantId = json['variant_id'];
        createdAt = json['created_at'];
        updatedAt = json['updated_at'];
        variant =
            json['variant'] != null ? new Variant.fromJson(json['variant']) : null;
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['id'] = this.id;
        data['product_id'] = this.productId;
        data['variant_id'] = this.variantId;
        data['created_at'] = this.createdAt;
        data['updated_at'] = this.updatedAt;
        if (this.variant != null) {
          data['variant'] = this.variant!.toJson();
        }
        return data;
      }
    }
    
    class Variant {
      int? id;
      String? name;
      int? attributeId;
      int? status;
      String? createdAt;
      String? updatedAt;
    
      Variant(
          {this.id,
          this.name,
          this.attributeId,
          this.status,
          this.createdAt,
          this.updatedAt});
    
      Variant.fromJson(Map<String, dynamic> json) {
        id = json['id'];
        name = json['name'];
        attributeId = json['attribute_id'];
        status = json['status'];
        createdAt = json['created_at'];
        updatedAt = json['updated_at'];
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['id'] = this.id;
        data['name'] = this.name;
        data['attribute_id'] = this.attributeId;
        data['status'] = this.status;
        data['created_at'] = this.createdAt;
        data['updated_at'] = this.updatedAt;
        return data;
      }
    }