Home > OS >  How can i handle multiple data type for a same key from API in Flutter?
How can i handle multiple data type for a same key from API in Flutter?

Time:11-28

Let's explain my problem.. suppose have a json object like

from this picture i want to handle offer_price key enter image description here

 {
  "product": [
    {
      "id": 1,
      "price": 100.0,
      "offer_price": 40
    },
    {
      "id": 2,
      "price": 80.0,
      "offer_price": 10.50
    },
    {
      "id": 3,
      "price": 200.0,
      "offer_price": "40.5"
    },
    {
      "id": 4,
      "price": 100.0,
      "offer_price": null,
      
    }
  ]
}

CodePudding user response:

class Product {
  int? id;
  int? price;

  // if you need the value as String
  String? offerPriceAsString;

  // Value as a double
  double? offerPrice;

  Product({this.id, this.price, this.offerPrice});

  Product.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    price = json['price'];
    double.parse("1.0");
    // Any value to String
    offerPriceAsString = json['offer_price'].toString();
    // String to Double
    offerPrice = double.parse(json['offer_price'].toString());
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    data['price'] = price;
    data['offer_price'] = offerPrice;
    return data;
  }
}

CodePudding user response:

In the dart data model for offer_price field make its datatype as dynamic via which you'll be able to have a dynamic data in it as run type. And while consuming this in any place just check the runtimeType of the variable and use it by type casting or just using it with .toString().

eg class:

class Products {
  Products({this.product});

  Products.fromJson(Map<String, dynamic> json) {
    if (json['product'] != null) {
      product = <Product>[];
      json['product'].forEach((v) {
        product!.add(Product.fromJson(v));
      });
    }
  }
  
  List<Product>? product;
  
  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    if (this.product != null) {
      data['product'] = this.product!.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Product {
  Product({this.id, this.price, this.offerPrice});
  
  Product.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    price = json['price'];
    offerPrice = json['offer_price'];
  }
  
  int? id;
  int? price;
  dynamic? offerPrice;


  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = this.id;
    data['price'] = this.price;
    data['offer_price'] = this.offerPrice;
    return data;
  }
}

For consuming it just try:

Products products = List<Products>[];

//assign products = Products.fromJson(jsonData);

//using
products[i].offerPrice == null
? "null"
: products[i].offerPrice.runtimeType == String
   ? products[i].offerPrice.toString()
   : products[i].offerPrice.runtimeType == Int
      ? int.parse(products[i].offerPrice)
      : products[i].offerPrice.runtimeType == Double
           ? double.parse(products[i].offerPrice)
           : ""
  • Related