Created
January 27, 2022 04:24
-
-
Save McCannDahl/fd7ef4ad358d2230ee3b4c4daa9775a5 to your computer and use it in GitHub Desktop.
Flutter Provider cheatsheet
This file contains 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
1. import provider package - https://pub.dev/packages/provider | |
2. create a change notifier | |
class CartModel extends ChangeNotifier { | |
final List<Item> _items = []; | |
void add(Item item) { | |
_items.add(item); | |
notifyListeners(); | |
} | |
} | |
3. Run app with provider | |
runApp( | |
MultiProvider( | |
providers: [ | |
ChangeNotifierProvider(create: (context) => CartModel()), | |
], | |
child: const MyApp(), | |
), | |
); | |
4. Call functions | |
Provider.of<CartModel>(context, listen: false).removeAll(); | |
5. Get data | |
Provider.of<CartModel>(context)._items; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment