mirror of
https://github.com/flutter/samples.git
synced 2025-11-08 13:58:47 +00:00
Follow best practice of ProxyProvider updating (#298)
* Upgrade to pkg:provider 4.0 * Follow best practice of ProxyProvider updating The addresses feedback from https://github.com/flutter/samples/issues/245. Instead of constructing a new `CartModel`, we merely update the `catalog` field. We no longer need a fancy constructor, and `CartModel._catalog` cannot be final any more.
This commit is contained in:
@@ -27,9 +27,11 @@ class MyApp extends StatelessWidget {
|
|||||||
// of ChangeNotifierProvider. Moreover, CartModel depends
|
// of ChangeNotifierProvider. Moreover, CartModel depends
|
||||||
// on CatalogModel, so a ProxyProvider is needed.
|
// on CatalogModel, so a ProxyProvider is needed.
|
||||||
ChangeNotifierProxyProvider<CatalogModel, CartModel>(
|
ChangeNotifierProxyProvider<CatalogModel, CartModel>(
|
||||||
create: (context) => CartModel.empty(),
|
create: (context) => CartModel(),
|
||||||
update: (context, catalog, previousCart) =>
|
update: (context, catalog, cart) {
|
||||||
CartModel(catalog, previousCart),
|
cart.catalog = catalog;
|
||||||
|
return cart;
|
||||||
|
},
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
child: MaterialApp(
|
child: MaterialApp(
|
||||||
|
|||||||
@@ -6,25 +6,25 @@ import 'package:flutter/foundation.dart';
|
|||||||
import 'package:provider_shopper/models/catalog.dart';
|
import 'package:provider_shopper/models/catalog.dart';
|
||||||
|
|
||||||
class CartModel extends ChangeNotifier {
|
class CartModel extends ChangeNotifier {
|
||||||
/// The current catalog. Used to construct items from numeric ids.
|
/// The private field backing [catalog].
|
||||||
final CatalogModel _catalog;
|
CatalogModel _catalog;
|
||||||
|
|
||||||
/// Internal, private state of the cart. Stores the ids of each item.
|
/// Internal, private state of the cart. Stores the ids of each item.
|
||||||
final List<int> _itemIds;
|
final List<int> _itemIds = [];
|
||||||
|
|
||||||
/// Construct a CartModel instance that is backed by a [CatalogModel] and
|
/// The current catalog. Used to construct items from numeric ids.
|
||||||
/// an optional previous state of the cart.
|
CatalogModel get catalog => _catalog;
|
||||||
///
|
|
||||||
/// If [previous] is not `null`, its items are copied to the newly
|
|
||||||
/// constructed instance.
|
|
||||||
CartModel(this._catalog, CartModel previous)
|
|
||||||
: assert(_catalog != null),
|
|
||||||
_itemIds = previous?._itemIds ?? [];
|
|
||||||
|
|
||||||
/// An empty cart with no Catalog.
|
set catalog(CatalogModel newCatalog) {
|
||||||
CartModel.empty()
|
assert(newCatalog != null);
|
||||||
: _catalog = null,
|
assert(_itemIds.every((id) => newCatalog.getById(id) != null),
|
||||||
_itemIds = [];
|
'The catalog $newCatalog does not have one of $_itemIds in it.');
|
||||||
|
_catalog = newCatalog;
|
||||||
|
// Notify listeners, in case the new catalog provides information
|
||||||
|
// different from the previous one. For example, availability of an item
|
||||||
|
// might have changed.
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
|
||||||
/// List of items in the cart.
|
/// List of items in the cart.
|
||||||
List<Item> get items => _itemIds.map((id) => _catalog.getById(id)).toList();
|
List<Item> get items => _itemIds.map((id) => _catalog.getById(id)).toList();
|
||||||
|
|||||||
@@ -88,6 +88,13 @@ packages:
|
|||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.1.8"
|
version: "1.1.8"
|
||||||
|
nested:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: nested
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "0.0.4"
|
||||||
path:
|
path:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@@ -115,7 +122,7 @@ packages:
|
|||||||
name: provider
|
name: provider
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "3.2.0"
|
version: "4.0.2"
|
||||||
quiver:
|
quiver:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@@ -193,3 +200,4 @@ packages:
|
|||||||
version: "3.5.0"
|
version: "3.5.0"
|
||||||
sdks:
|
sdks:
|
||||||
dart: ">=2.5.0 <3.0.0"
|
dart: ">=2.5.0 <3.0.0"
|
||||||
|
flutter: ">=1.12.1"
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ dependencies:
|
|||||||
sdk: flutter
|
sdk: flutter
|
||||||
|
|
||||||
# Import the provider package.
|
# Import the provider package.
|
||||||
provider: ^3.1.0
|
provider: ^4.0.2
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|||||||
Reference in New Issue
Block a user