Coupon.fromJson constructor

Coupon.fromJson(
  1. Map<String, dynamic> json
)

Implementation

Coupon.fromJson(Map<String, dynamic> json) {
  try {
    amount = double.parse(json['amount'].toString());
    status = json['status'];
    code = json['code'];
    id = json['id']?.toString();
    discountType = json['discount_type'];
    description = json['description'];
    minimumAmount = json['minimum_amount'] != null
        ? double.parse(json['minimum_amount'].toString())
        : 0.0;
    maximumAmount = json['maximum_amount'] != null
        ? double.parse(json['maximum_amount'].toString())
        : 0.0;
    dateExpires = json['date_expires'] != null
        ? DateTime.parse(json['date_expires'])
        : null;
    message = '';
    usageCount = json['usage_count'];
    individualUse = json['individual_use'] ?? false;
    usageLimit = json['usage_limit'];
    usageLimitPerUser = json['usage_limit_per_user'];
    freeShipping = json['free_shipping'] ?? false;
    excludeSaleItems = json['exclude_sale_items'] ?? false;

    if (json['product_ids'] != null) {
      productIds = [];
      if (json['product_ids'] is List) {
        json['product_ids'].forEach((e) {
          productIds.add(e.toString());
        });
      } else if (json['product_ids'] is Map) {
        json['product_ids'].forEach((key, value) {
          productIds.add(value.toString());
        });
      }
    }

    if (json['excluded_product_ids'] != null) {
      excludedProductIds = [];
      if (json['excluded_product_ids'] is List) {
        json['excluded_product_ids'].forEach((e) {
          excludedProductIds.add(e.toString());
        });
      } else if (json['excluded_product_ids'] is Map) {
        json['excluded_product_ids'].forEach((key, value) {
          excludedProductIds.add(value.toString());
        });
      }
    }

    if (json['product_categories'] != null) {
      productCategories = [];
      if (json['product_categories'] is List) {
        json['product_categories'].forEach((e) {
          productCategories.add(e.toString());
        });
      } else if (json['product_categories'] is Map) {
        json['product_categories'].forEach((key, value) {
          productCategories.add(value.toString());
        });
      }
    }

    if (json['excluded_product_categories'] != null) {
      excludedProductCategories = [];
      if (json['excluded_product_categories'] is List) {
        json['excluded_product_categories'].forEach((e) {
          excludedProductCategories.add(e.toString());
        });
      } else if (json['excluded_product_categories'] is Map) {
        json['excluded_product_categories'].forEach((key, value) {
          excludedProductCategories.add(value.toString());
        });
      }
    }

    if (json['email_restrictions'] != null) {
      emailRestrictions = [];
      if (json['email_restrictions'] is List) {
        json['email_restrictions'].forEach((e) {
          emailRestrictions.add(e.toString());
        });
      } else if (json['email_restrictions'] is Map) {
        json['email_restrictions'].forEach((key, value) {
          emailRestrictions.add(value.toString());
        });
      }
    }

    if (json['used_by'] != null) {
      usedBy = [];
      if (json['used_by'] is List) {
        json['used_by'].forEach((e) {
          usedBy?.add(e.toString());
        });
      } else if (json['used_by'] is Map) {
        json['used_by'].forEach((key, value) {
          usedBy?.add(value.toString());
        });
      }
    }
  } catch (e) {
    // ignore: avoid_print
    print(e.toString());
  }
}