text
stringlengths
1
372
}
class _MyHomepageState extends State<MyHomepage> {
int _index = 0;
@override
widget build(BuildContext context) {
return BottomNavigationBar(
currentIndex: _index,
onTap: (newindex) {
setState(() {
_index = newIndex;
});
},
// ... items ...
);
}
}
<code_end>
here, using setState() and a field inside the StatefulWidget’s state
class is completely natural. no other part of your app needs to access
_index. the variable only changes inside the MyHomepage widget.
and, if the user closes and restarts the app,
you don’t mind that _index resets to zero.
<topic_end>
<topic_start>
app state
state that is not ephemeral,
that you want to share across many parts of your app,
and that you want to keep between user sessions,
is what we call application state
(sometimes also called shared state).
examples of application state:
for managing app state, you’ll want to research your options.
your choice depends on the complexity and nature of your app,
your team’s previous experience, and many other aspects. read on.
<topic_end>
<topic_start>
there is no clear-cut rule
to be clear, you can use state and setState() to manage all of
the state in your app. in fact, the flutter team does this in many
simple app samples (including the starter app that you get with every
flutter create).
it goes the other way, too. for example, you might decide that—in
the context of your particular app—the selected tab in a bottom
navigation bar is not ephemeral state. you might need to change it
from outside the class, keep it between sessions, and so on.
in that case, the _index variable is app state.
there is no clear-cut, universal rule to distinguish
whether a particular variable is ephemeral or app state.
sometimes, you’ll have to refactor one into another.
for example, you’ll start with some clearly ephemeral state,
but as your application grows in features,
it might need to be moved to app state.
for that reason, take the following diagram with a large grain of salt:
when asked about react’s setState versus redux’s store, the author of redux,
dan abramov, replied:
“the rule of thumb is: do whatever is less awkward.”
in summary, there are two conceptual types of state in any flutter app.
ephemeral state can be implemented using state and setState(),
and is often local to a single widget. the rest is your app state.
both types have their place in any flutter app, and the split between
the two depends on your own preference and the complexity of the app.
<topic_end>
<topic_start>
simple app state management
now that you know about declarative UI programming
and the difference between ephemeral and app state,
you are ready to learn about simple app state management.
on this page, we are going to be using the provider package.
if you are new to flutter and you don’t have a strong reason to choose
another approach (redux, rx, hooks, etc.), this is probably the approach
you should start with. the provider package is easy to understand
and it doesn’t use much code.
it also uses concepts that are applicable in every other approach.
that said, if you have a strong background in
state management from other reactive frameworks,
you can find packages and tutorials listed on the options page.
<topic_end>
<topic_start>
our example
for illustration, consider the following simple app.
the app has two separate screens: a catalog,
and a cart (represented by the MyCatalog,
and MyCart widgets, respectively). it could be a shopping app,
but you can imagine the same structure in a simple social networking
app (replace catalog for “wall” and cart for “favorites”).
the catalog screen includes a custom app bar (myappbar)
and a scrolling view of many list items (mylistitems).
here’s the app visualized as a widget tree.
so we have at least 5 subclasses of widget. many of them need
access to state that “belongs” elsewhere. for example, each
MyListItem needs to be able to add itself to the cart.
it might also want to see whether the currently displayed item
is already in the cart.
this takes us to our first question: where should we put the current
state of the cart?
<topic_end>
<topic_start>
lifting state up
in flutter,
it makes sense to keep the state above the widgets that use it.