Created
August 8, 2018 06:45
-
-
Save rohan20/fd43683c823a5c798935e532d4958a5e to your computer and use it in GitHub Desktop.
E-Commerce app using Flutter - Part 4: Scoped Model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Future parseProductsFromResponse(int categoryId, int pageIndex) async { | |
if (pageIndex == 1) { | |
_isLoading = true; | |
} | |
notifyListeners(); | |
currentProductCount = 0; | |
var dataFromResponse = await _getProductsByCategory(categoryId, pageIndex); | |
dataFromResponse.forEach( | |
(newProduct) { | |
currentProductCount++; | |
//parse the product's images | |
List<AnyImage> imagesOfProductList = []; | |
newProduct["images"].forEach( | |
(newImage) { | |
imagesOfProductList.add( | |
new AnyImage( | |
imageURL: newImage["src"], | |
id: newImage["id"], | |
title: newImage["name"], | |
alt: newImage["alt"], | |
), | |
); | |
}, | |
); | |
//parse the product's categories | |
List<Category> categoriesOfProductList = []; | |
newProduct["categories"].forEach( | |
(newCategory) { | |
categoriesOfProductList.add( | |
new Category( | |
id: newCategory["id"], | |
name: newCategory["name"], | |
), | |
); | |
}, | |
); | |
//parse new product's details | |
Product product = new Product( | |
productId: newProduct["id"], | |
productName: newProduct["name"], | |
description: newProduct["description"], | |
regularPrice: newProduct["regular_price"], | |
salePrice: newProduct["sale_price"], | |
stockQuantity: newProduct["stock_quantity"] != null | |
? newProduct["stock_quantity"] | |
: 0, | |
ifItemAvailable: newProduct["on_sale"] && | |
newProduct["purchasable"] && | |
newProduct["in_stock"], | |
discount: ((((int.parse(newProduct["regular_price"]) - | |
int.parse(newProduct["sale_price"])) / | |
(int.parse(newProduct["regular_price"]))) * | |
100)) | |
.round(), | |
images: imagesOfProductList, | |
categories: categoriesOfProductList, | |
); | |
addToProductsList(product); | |
}, | |
); | |
if (pageIndex == 1) _isLoading = false; | |
if (currentProductCount < 6) { | |
_hasModeProducts = false; | |
} | |
notifyListeners(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment