text
stringlengths
1
474
there comes a time when you need to share application
state between screens, across your app.
There are many approaches you can take,
and many questions to think about.In the following pages,
you will learn the basics of dealing with state in Flutter apps.
<topic_end>
<topic_start>Start thinking declaratively
If you’re coming to Flutter from an imperative framework
(such as Android SDK or iOS UIKit), you need to start
thinking about app development from a new perspective.Many assumptions that you might have don’t apply to Flutter. For example, in
Flutter it’s okay to rebuild parts of your UI from scratch instead of modifying
it. Flutter is fast enough to do that, even on every frame if needed.Flutter is declarative. This means that Flutter builds its user interface to
reflect the current state of your app:When the state of your app changes
(for example, the user flips a switch in the settings screen),
you change the state, and that triggers a redraw of the user interface.
There is no imperative changing of the UI itself
(like widget.setText)—you change the state,
and the UI rebuilds from scratch.Read more about the declarative approach to UI programming
in the get started guide.The declarative style of UI programming has many benefits.
Remarkably, there is only one code path for any state of the UI.
You describe what the UI should look
like for any given state, once—and that is it.At first,
this style of programming might not seem as intuitive as the
imperative style. This is why this section is here. Read on.
<topic_end>
<topic_start>Differentiate between ephemeral state and app state
This doc introduces app state, ephemeral state,
and how you might manage each in a Flutter app.In the broadest possible sense, the state of an app is everything that
exists in memory when the app is running. This includes the app’s assets,
all the variables that the Flutter framework keeps about the UI,
animation state, textures, fonts, and so on. While this broadest
possible definition of state is valid, it’s not very useful for
architecting an app.First, you don’t even manage some state (like textures).
The framework handles those for you. So a more useful definition of
state is “whatever data you need in order to rebuild your UI at any
moment in time”. Second, the state that you do manage yourself can
be separated into two conceptual types: ephemeral state and app state.<topic_end>
<topic_start>
Ephemeral state
Ephemeral state (sometimes called UI state or local state)
is the state you can neatly contain in a single widget.This is, intentionally, a vague definition, so here are a few examples.Other parts of the widget tree seldom need to access this kind of state.
There is no need to serialize it, and it doesn’t change in complex ways.In other words, there is no need to use state management techniques
(ScopedModel, Redux, etc.) on this kind of state.
All you need is a StatefulWidget.Below, you see how the currently selected item in a bottom navigation bar is
held in the _index field of the _MyHomepageState class.
In this example, _index is ephemeral state.
<code_start>class MyHomepage extends StatefulWidget {
const MyHomepage({super.key});
@override
State<MyHomepage> createState() => _MyHomepageState();
}
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.