Created
October 24, 2021 15:54
-
-
Save perymerdeka/dbd8f4bb05e5302254fcfd1f6c97f0f2 to your computer and use it in GitHub Desktop.
provider for
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
import 'dart:math'; | |
import 'package:flutter/material.dart'; | |
import 'package:providers/models/product.dart'; | |
class ProductProvider with ChangeNotifier { | |
final List<Product> _products = List.generate( | |
25, | |
(index) { | |
return Product( | |
id: "id_${index + 1}", | |
title: "Product ${index + 1}", | |
description: 'Ini adalah deskripsi produk ${index + 1}', | |
price: 10 + Random().nextInt(100).toDouble(), | |
imageUrl: 'https://picsum.photos/id/$index/200/300', | |
); | |
}, | |
); | |
// getter | |
List<Product> get products { | |
return [..._products]; | |
} | |
Product findById(productId) { | |
return _products.firstWhere((prod) => prod.id == productId); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment