Skip to content

Instantly share code, notes, and snippets.

@perymerdeka
Created October 24, 2021 15:54
Show Gist options
  • Save perymerdeka/dbd8f4bb05e5302254fcfd1f6c97f0f2 to your computer and use it in GitHub Desktop.
Save perymerdeka/dbd8f4bb05e5302254fcfd1f6c97f0f2 to your computer and use it in GitHub Desktop.
provider for
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