articles_model.dart 1.47 KB
Newer Older
yenisleydi committed
1
class ArticlesModel {
yenisleydi committed
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
  final int id;
  final String clave;
  final String nombre;
  final int categoriaId;
  final List<Price> precios;
  final bool activo;

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

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

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

yenisleydi committed
32 33 34 35 36 37 38 39 40 41 42
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'clave': clave,
      'nombre': nombre,
      'categoriaId': categoriaId,
      'precios': precios.map((price) => price.toJson()).toList(),
      'activo': activo,
    };
  }

yenisleydi committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
  static List<ArticlesModel> fromJsonArray(List<dynamic> jsonArray) {
    return jsonArray.map((json) => ArticlesModel.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(),
    );
  }
yenisleydi committed
63 64 65 66 67 68 69 70

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'precio': precio,
    };
  }
}