Creating a shopping cart application with InheritedModel
This will be very similar to InheritedWidget
, with slight changes in how we extract the latest values of the variables. Create a new app named cart_inherited_model
and copy everything from the previous section. Then, proceed as follows:
- Let's update our boilerplate code first. Rename the
inherited_widget_cart.dart
fileinherited_model_cart.dart
, remove all the contents, and add the following code:import 'package:flutter/material.dart'; import 'item.dart'; class MyCartInheritedModelWidget extends StatefulWidget { final Widget child; const MyCartInheritedModelWidget({Key? key, required this.child}) : super(key: key); static MyCartInheritedModelWidgetState of(BuildContext context) { final MyCartInheritedModelWidgetState? result = context.dependOnInheritedWidgetOfExactType...