Item.fromJson constructor

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

Converts a JSON object into an Item object.

Expects the following JSON format:

{
  "name": "Pizza",
  "quantity": 2,
  "price": 10.5,
  "category": "food"
}

Implementation

factory Item.fromJson(Map<String, dynamic> json) {
  return Item(
    name: json['name'],
    quantity: json['quantity'].toInt(),
    price: (json['price'].runtimeType != String)
        ? json['price'].toDouble()
        : 0, // Ensures correct parsing for non-string price fields
    category: json['category'].toString().toLowerCase(),
  );
}