Order.fromJson constructor

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

Converts a JSON object into an Order object.

Expects the following JSON format:

{
  "items": [
    {
      "name": "Item Name",
      "quantity": 2,
      "price": 10.5,
      "category": "food"
    }
  ],
  "subtotal": 21.0,
  "tax": 2.1,
  "total": 23.1
}

Implementation

factory Order.fromJson(Map<String, dynamic> json) {
  return Order(
    date: json['date'],
    invoiceNumber: json['invoice_number'],
    paymentMethod: json['payment_method'].toString().toUpperCase(),
    items: (json['items'] as List<dynamic>)
        .map((itemJson) => Item.fromJson(itemJson))
        .toList(),
    subtotal: json['subtotal'].toDouble(),
    tax: json['tax'].toDouble(),
    total: json['total'].toDouble(),
  );
}