ArticuloModel.dart 1.15 KB
Newer Older
nayeli92433 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
import 'dart:convert';

class ArticleModel {
  final int id;
  final String clave;
  final String nombre;
  final int categoriaId;
  final List<Price> precios;
  final bool activo;

  ArticleModel({
    required this.id,
    required this.clave,
    required this.nombre,
    required this.categoriaId,
    required this.precios,
    required this.activo,
  });

  factory ArticleModel.fromJson(Map<String, dynamic> json) {
    var list = json['precios'] as List;
    List<Price> preciosList = list.map((i) => Price.fromJson(i)).toList();

    return ArticleModel(
      id: json['id'],
      clave: json['clave'],
      nombre: json['nombre'],
      categoriaId: json['categoria']['id'],
      precios: preciosList,
      activo: json['activo'],
    );
  }

  static List<ArticleModel> fromJsonArray(List<dynamic> jsonArray) {
    return jsonArray.map((json) => ArticleModel.fromJson(json)).toList();
  }
}

class Price {
  final int id;
  final double precio;

  Price({
    required this.id,
    required this.precio,
  });

  factory Price.fromJson(Map<String, dynamic> json) {
    return Price(
      id: json['id'],
      precio: json['precio'].toDouble(),
    );
  }
}