Flutter State Management
State management is a core concept in Flutter development.
This section introduces Flutter's built-in state management approaches, including setState, Provider, and ChangeNotifier.
What is state management
State management refers to the mechanism that manages data changes and UI updates in an app. When data changes, the UI needs to update accordingly, which is the problem state management solves.
| State types | Description | Example |
|---|---|---|
| Local state | State within a single Widget | Input box text, animation progress |
| Shared state | State that multiple Widgets need to share | User login info, theme |
| App state | State that the entire app needs to access | Shopping cart contents, user settings |
Local state - setState
For simple local state, using StatefulWidget and setState is sufficient.
Example: counter example
class
CounterWidget
extends
StatefulWidget
{
const
CounterWidget
(
{
super
.
key
}
)
;
@override
State
<
CounterWidget
createState
(
)
=>
_CounterWidgetState
(
);
}
class
_CounterWidgetState
extends
State
<
CounterWidget
{// local state
int
_count
=
0
;
// increment the count
void
_increment
(
)
{
setState
(
(
)
{
_count
++
;
}
)
;
}
// decrement the count
void
_decrement
(
)
{
setState
(
(
)
{
_count
--
;
}
)
;
}
@override
Widget build
(
BuildContext context
)
{
return
Column
(
mainAxisAlignment
:
MainAxisAlignment.
center
,
children
:
[
Text
(
'Count: $_count'
,
style
:
const
TextStyle
(
fontSize
:
32
)
)
,
const
SizedBox
(
height
:
20
)
,
Row
(
mainAxisAlignment
:
MainAxisAlignment.
center
,
children
:
[
ElevatedButton
(
onPressed
:
_decrement
,
child
:
const
Text
(
'-'
)
)
,
const
SizedBox
(
width
:
20
)
,
ElevatedButton
(
onPressed
:
_increment
,
child
:
const
Text
(
'+'
)
)
,
]
,
)
,
]
,
)
;
}
}
Provider state management
Provider is one of Flutter's officially recommended state management solutions, suitable for managing state shared across components.
Install Provider
Add the dependency in pubspec.yaml:
dependencies:
provider: ^6.0.0
ChangeNotifier pattern
Example: Provider + ChangeNotifier
// 1. Define the data model (extends ChangeNotifier)
class
CounterModel
extends
ChangeNotifier
{
int
_count
=
0
;
int
get
count
=>
_count
;
void
increment
(
)
{
_count
++
;
// Notify listeners that the state has changed
notifyListeners
(
)
;
}
void
decrement
(
)
{
_count
--
;
notifyListeners
(
)
;
}
}
// 2. Provide data at the top level of the app
void
main
(
)
{
runApp
(
// Provide CounterModel to child components
ChangeNotifierProvider
(
create
:
(
context
)
=>
CounterModel
(
)
,
child
:
const
MyApp
(
)
,
)
,
)
;
}
// 3. Use the data in components
class
CounterDisplay
extends
StatelessWidget
{
const
CounterDisplay
(
{
super
.
key
}
)
;
@override
Widget build
(
BuildContext context
)
{
// Read CounterModel
final
counter
=
context.
watch
<
CounterModel
(
);
return
Column
(
children
:
[
Text
(
'Count: ${counter.count}'
,
style
:
const
TextStyle
(
fontSize
:
32
)
)
,
ElevatedButton
(
onPressed
:
(
)
=>
counter.
increment
(
)
,
child
:
const
Text
(
'Increment'
)
,
)
,
]
,
)
;
}
}
Multiple Provider composition
Example: using multiple Providers
void
main
(
)
{
runApp
(
MultiProvider
(
providers
:
[
// User info Provider
ChangeNotifierProvider
(
create
:
(
_
)
=>
UserModel
(
)
)
,
// Theme Provider
ChangeNotifierProvider
(
create
:
(
_
)
=>
ThemeModel
(
)
)
,
// Shopping cart Provider
ChangeNotifierProvider
(
create
:
(
_
)
=>
CartModel
(
)
)
,
]
,
child
:
const
MyApp
(
)
,
)
,
)
;
}
// Use context.read when you don't need to listen for changes
class
SomeWidget
extends
StatelessWidget
{
const
SomeWidget
(
{
super
.
key
}
)
;
@override
Widget build
(
BuildContext context
)
{
// Read without listening
final
user
=
context.
read
<
UserModel
(
);
return
ElevatedButton
(
onPressed
:
(
)
=>
user.
logout
(
)
,
child
:
const
Text
(
'Log out'
)
,
)
;
}
}
State lifting - Lifting State Up
When multiple child components need to share state, the state can be lifted to the nearest common parent component.
Example: state lifting
// Parent component manages state
class
ParentWidget
extends
StatefulWidget
{
const
ParentWidget
(
{
super
.
key
}
)
;
@override
State
<
ParentWidget
createState
(
)
=>
_ParentWidgetState
(
);
}
class
_ParentWidgetState
extends
State
<
ParentWidget
{// Shared state
bool
_isEnabled
=
false
;
// Callback method
void
_toggleEnabled
(
)
{
setState
(
(
)
{
_isEnabled
=
!
_isEnabled
;
}
)
;
}
@override
Widget build
(
BuildContext context
)
{
return
Column
(
children
:
[
// Child component A
ChildWidgetA
(
isEnabled
:
_isEnabled
,
)
,
// Child component B
ChildWidgetB
(
isEnabled
:
_isEnabled
,
onToggle
:
_toggleEnabled
,
)
,
]
,
)
;
}
}
// Child component A (only displays state)
class
ChildWidgetA
extends
StatelessWidget
{
final
bool
isEnabled
;
const
ChildWidgetA
(
{
super
.
key
,
required
this
.
isEnabled
}
)
;
@override
Widget build
(
BuildContext context
)
{
return
Text
(
'State: ${isEnabled ? "on" : "off"}'
)
;
}
}
// Child component B (controls state)
class
ChildWidgetB
extends
StatelessWidget
{
final
bool
isEnabled
;
final
VoidCallback onToggle
;
const
ChildWidgetB
(
{
super
.
key
,
required
this
.
isEnabled
,
required
this
.
onToggle
,
}
)
;
@override
Widget build
(
BuildContext context
)
{
return
ElevatedButton
(
onPressed
:
onToggle
,
child
:
Text
(
isEnabled
?
'Off'
:
'On'
)
,
)
;
}
}
The choice of state management depends on the app size: simple apps use setState, medium apps use Provider, complex apps can consider solutions such as Riverpod, BLoC, or GetX.