Skip to content

Instantly share code, notes, and snippets.

@McCannDahl
Created January 27, 2022 04:24
Show Gist options
  • Save McCannDahl/fd7ef4ad358d2230ee3b4c4daa9775a5 to your computer and use it in GitHub Desktop.
Save McCannDahl/fd7ef4ad358d2230ee3b4c4daa9775a5 to your computer and use it in GitHub Desktop.
Flutter Provider cheatsheet
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