颤振模型类

时间:2020-09-24 08:54:42

标签: flutter dart product

这是什么类型的模型类?如何将其转换为动态模型类,以便从JSON API获取数据?请任何建议我解决方案? 我想为我的json API制作购物车页面。

import 'package:flutter/material.dart';
import './product.dart';

class Products with ChangeNotifier {
  List<Product> _items = [
    Product(
  id: 'p1',
  title: 'Red Shirt',
  description: 'A red shirt - it is pretty red!',
  price: 29.99,
  imageUrl:
      'https://cdn.pixabay.com/photo/2016/10/02/22/17/red-t-shirt-1710578_1280.jpg',
),
Product(
  id: 'p2',
  title: 'Trousers',
  description: 'A nice pair of trousers.',
  price: 59.99,
  imageUrl:
      'https://upload.wikimedia.org/wikipedia/commons/thumb/e/e8/Trousers%2C_dress_%28AM_1960.022-        
 8%29.jpg/512px-Trousers%2C_dress_%28AM_1960.022-8%29.jpg',
 ),
 Product(
  id: 'p3',
   title: 'Yellow Scarf',
   description: 'Warm and cozy - exactly what you need for the winter.',
   price: 19.99,
   imageUrl:
      'https://live.staticflickr.com/4043/4438260868_cc79b3369d_z.jpg',
),
Product(
  id: 'p4',
  title: 'A Pan',
  description: 'Prepare any meal you want.',
  price: 49.99,
  imageUrl:
      'https://upload.wikimedia.org/wikipedia/commons/thumb/1/14/Cast-Iron-Pan.jpg/1024px-Cast-Iron- 
  Pan.jpg',
 ),
 ];
 // var _showFavoritesOnly = false;

  List<Product> get items {
  // if (_showFavoritesOnly) {
  //   return _items.where((prodItem) => prodItem.isFavorite).toList();
  // }
 return [..._items];
 }

  List<Product> get favoriteItems {
  return _items.where((prodItem) => prodItem.isFavorite).toList();
 }

  Product findById(String id) {
  return _items.firstWhere((prod) => prod.id == id);
  }

  // void showFavoritesOnly() {
  //   _showFavoritesOnly = true;
      //   notifyListeners();
  // }

  // void showAll() {
  //   _showFavoritesOnly = false;
  //   notifyListeners();
  // }

   void addProduct() {
  // _items.add(value);
  notifyListeners();
  }
  }

我想使用这种类型的模型类:-

class Product {
int id;
String productName;
String productDescription;
int categoryId;
int colorId;
int sizeId;
int regularPrice;
int salePrice;
int quantity;
int ratting;
String productImage;
int flag;
int status;
String createdAt;
String updatedAt;

Product(
  {this.id,
  this.productName,
  this.productDescription,
  this.categoryId,
  this.colorId,
  this.sizeId,
  this.regularPrice,
  this.salePrice,
  this.quantity,
  this.ratting,
  this.productImage,
  this.flag,
  this.status,
  this.createdAt,
  this.updatedAt});

 Product.fromJson(Map<String, dynamic> json) {
id = json['id'];
productName = json['product_name'];
productDescription = json['product_description'];
categoryId = json['category_id'];
colorId = json['color_id'];
sizeId = json['size_id'];
regularPrice = json['regular_price'];
salePrice = json['sale_price'];
quantity = json['quantity'];
ratting = json['ratting'];
productImage = json['product_image'];
flag = json['flag'];
status = json['status'];
createdAt = json['created_at'];
updatedAt = json['updated_at'];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['product_name'] = this.productName;
data['product_description'] = this.productDescription;
data['category_id'] = this.categoryId;
data['color_id'] = this.colorId;
data['size_id'] = this.sizeId;
data['regular_price'] = this.regularPrice;
data['sale_price'] = this.salePrice;
data['quantity'] = this.quantity;
data['ratting'] = this.ratting;
data['product_image'] = this.productImage;
data['flag'] = this.flag;
data['status'] = this.status;
data['created_at'] = this.createdAt;
data['updated_at'] = this.updatedAt;
return data;
}
}

我知道参数不相同,但是我想从Json API获取数据。

0 个答案:

没有答案