Updated 2 May 2023
Flutter app state management using provider is a way to perform the UI update without building all widgets of the page.
Flutter app state management using provider helps us to make the application faster.
The provider work as a wrapper. it makes it easier to use with less boilerplate code.
Because the provider is very easy to, that’s why it removes the use of the set state.
So The use of the provider widgets needs to subscribe.
Then provider makes it easier to listen to the changes in widgets through the model using notifier.
You may also check our flutter app development services
There are three components related to this provider app state management.
A class that extends ChangeNotifier
 can call notifyListeners()
 any time data in that class has been updated and you want to let a listener know about that update.
Change notifier which listened to by the change notifier provider.
provider installation clicks here.
1 2 |
dependencies: provider: ^5.0.0 |
We will use the counter update on button click using provider without using the set state function.
first, we will create the model of counter update class that will extend the change notifier.
1 2 3 4 5 6 7 8 9 10 |
class counterUpdateModel extends ChangeNotifier{ var count = 0; void counterUpdate() { count += 1; notifyListeners(); } } Here notify listeners will notify the consumer for update the widget. |
The ChangeNotifierProvider<T extends ChangeNotifier> It listens to a ChangeNotifier extended by the model class.
This provides the help to exposes it to its children and descendants and rebuilds the view when change notify lister called.
1 2 3 4 5 6 7 8 |
void main() { runApp( ChangeNotifierProvider( create: (context) => CounterUpdateModel(), child: CounterUpdate(), ), ); } |
The consumer is the widget that we use to listen to the change notifier update.
So this provides the model instance for use and updates the model.
Below the full code of counter update example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
import 'dart:developer'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; class CounterUpdate extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: Text('Counter update'), ), body: Container( alignment: Alignment.center, child: Consumer<CounterUpdateModel>( builder: (context, counter, child) { return Container( height: 200, width: 200, child: Column( children: [ Container( padding: EdgeInsets.only(top: 25), child: Text(counter.count.toString()), ), SizedBox(height: 16,), GestureDetector( onTap: () { counter.counterUpdate(); }, child: Container( height: 40, width: 120, child: Center( child: Container( height: 40, color: Colors.black, child: Center( child: Text( 'Counter update', style: TextStyle( color: Colors.white ), ), ), ), ), ), ) ], ), ); }, ), ), ), ); } } class CounterUpdateModel extends ChangeNotifier { var count = 0; void counterUpdate() { count += 1; notifyListeners(); } } |
And thanks for reading this blog, for detail info please click here
For more blog please click here.
So pls follow the above step and And if you have any issue or suggestion you can leave your message in the comment section I will try to solve this.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
2 comments
Your welcome