Advertisement
majjin

Product Class

Oct 6th, 2020
1,149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 1.41 KB | None | 0 0
  1. import 'package:hive/hive.dart';
  2.  
  3. @HiveType(typeId: 1)
  4. class Product {
  5.   @HiveField(0)
  6.   final int id;
  7.  
  8.   @HiveField(1)
  9.   final String name;
  10.  
  11.   @HiveField(2)
  12.   final String description;
  13.  
  14.   @HiveField(3)
  15.   final double size;
  16.  
  17.   @HiveField(4)
  18.   final String sizeUnit;
  19.  
  20.   @HiveField(5)
  21.   final double price;
  22.  
  23.   @HiveField(6)
  24.   final bool byWeight;
  25.  
  26.   @HiveField(7)
  27.   final int quantity;
  28.  
  29.   @HiveField(8)
  30.   final String imageStr;
  31.  
  32.   @HiveField(9)
  33.   final int category;
  34.  
  35.   @HiveField(10)
  36.   int _cartQuantity = 0;
  37.  
  38.   Product(this.id, this.name, this.description, this.size, this.sizeUnit,
  39.       this.price, this.byWeight, this.quantity, this.imageStr, this.category);
  40.  
  41.   factory Product.fromJson(Map<String, dynamic> json) {
  42.     return Product(
  43.         json['Id'],
  44.         json['Name'],
  45.         json['Description'],
  46.         json['Size'] is int ? (json['Size'] as int).toDouble() : json['Size'],
  47.         json['Size_Unit'],
  48.         json['Price'] is int
  49.             ? (json['Price'] as int).toDouble()
  50.             : json['Price'],
  51.         json['ByWeight'] as int != 0,
  52.         json['Quantity'],
  53.         json['Image'],
  54.         json['Category']);
  55.   }
  56.  
  57.   void increaseQuantity() {
  58.     if (_cartQuantity < quantity) {
  59.       ++_cartQuantity;
  60.     }
  61.   }
  62.  
  63.   void decreaseQuantity() {
  64.     if (_cartQuantity > 0) {
  65.       --_cartQuantity;
  66.     }
  67.   }
  68.  
  69.   int getCartQuantity() => _cartQuantity;
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement