0% found this document useful (0 votes)
3 views

Exp 5b State Management in Flutter

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Exp 5b State Management in Flutter

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

State Management

ACE ENGINEERING COLLEGE Jayasri


2

What is State Management?


• Flutter, the state is any information that can affect
the visual appearance of a widget.

SDC – Flutter – IIIrd Year


• Flutter state management refers to how you
handle and maintain the state of your application,
ensuring that the user interface (UI) reflects the
current state of the application consistently.

ACE ENGINEERING COLLEGE Jayasri


3

Types of State in Flutter


• 1. Ephemeral (Local) State: Confined to a single
widget.

SDC – Flutter – IIIrd Year


–Counter Application
• 2. App-wide (Global) State: Shared across the
application.
–Login info
–The cart in an e-commerce app
–Notifications of the apps

ACE ENGINEERING COLLEGE Jayasri


4
4

Ways to Approach State


management
• Provider • GetIt
• setState • MobX

SDC – Flutter – IIIrd Year


• InheritedWidget and • Binder
InheritedModel • GetX
• Redux and Fish-Redux • Riverpod
• BLoc/Rx

ACE ENGINEERING COLLEGE Jayasri


5

setState: Built-in State Management


• Flutter's default mechanism for managing local
state.

SDC – Flutter – IIIrd Year


• Simple, intuitive, and suitable for small
applications.
• In the case of a stateful widget
the setState() method is used to change the state
and that forces the UI to rebuild.

ACE ENGINEERING COLLEGE Jayasri


6

Example: setState
class CounterApp extends StatefulWidget {
int _count = 0;

SDC – Flutter – IIIrd Year


void _increment() {
setState(() { _count++; });
}
}

ACE ENGINEERING COLLEGE Jayasri


7

Limitations of setState
1. Inefficient for sharing state across widgets.
2. Not scalable for large applications.

SDC – Flutter – IIIrd Year


ACE ENGINEERING COLLEGE Jayasri
8
Provider: A State Management
Solution
To understand provider package the three basic
concepts need are:

SDC – Flutter – IIIrd Year


1.ChangeNotifier
2.ChangeNotifierProvider
3.Consumer

ACE ENGINEERING COLLEGE Jayasri


99

ChangeNotifier
• It is a class that provides notifications for changes
to its listeners.

SDC – Flutter – IIIrd Year


• It is a simpler way to use for a small number of
listeners.
• It uses notifyListeners() method to notify its
listeners about changes in the model.

ACE ENGINEERING COLLEGE Jayasri


1010

ChangeNotifierProvider
• ChangeNotifierProvider is just a widget that
provides the instance of a ChangeNotifier.

SDC – Flutter – IIIrd Year


ACE ENGINEERING COLLEGE Jayasri
1111

Consumer
• It is a widget that contains a builder function and is
used to build the UI based on changes in the model.

SDC – Flutter – IIIrd Year


• The builder function will have three parameters
context, counter, and child.
– context is the same as every other build function
of the widget.
–The counter is the CounterModel member that
was observed for change.
–The third argument child is used for optimization.

ACE ENGINEERING COLLEGE Jayasri


12

Provider Example
class Counter with ChangeNotifier {
int _count = 0;

SDC – Flutter – IIIrd Year


void increment() {
_count++;
notifyListeners();
}
}
ACE ENGINEERING COLLEGE Jayasri
13

Conclusion
• Choose the right state management solution
based on app complexity and requirements:

SDC – Flutter – IIIrd Year


• - Small apps: setState
• - Medium to large apps: Provider or Riverpod
• - Complex apps: Bloc or Redux

ACE ENGINEERING COLLEGE Jayasri

You might also like