- Its better to priority to use
StatelessWidget
in all flutter App. but also when you need to have a initial state, or local state variable, its will be better to use StatefullWidget
. So... use as you need.
- I would said that using
ChangeNotifierProvider
on top of tree is a common practice.
see here official example from docs.flutter.dev, they put inside main function.
https://docs.flutter.dev/development/data-and-backend/state-mgmt/simple#changenotifierprovider
void main() {
runApp(
ChangeNotifierProvider(
create: (context) => CartModel(),
child: const MyApp(),
),
);
}
;TLDR
the things that we have to avoid while using provider is with the Consumer
at the top of widget tree.
read here : https://docs.flutter.dev/development/data-and-backend/state-mgmt/simple#consumer
It is best practice to put your Consumer widgets as deep in the tree
as possible. You don’t want to rebuild large portions of the UI just
because some detail somewhere changed.
when we called the notifyListeners();
this will rebuild current Consumer
widget. when you use it on Top of tree, everytime receive notifylistener your widget Tree will rebuild from the Consumer below.
You can see the example here : https://docs.flutter.dev/development/data-and-backend/state-mgmt/simple#consumer