provider-state-management-in-flutter

How to use Provider State Management in Flutter

Share this post on:

State management is a critical part of any user application’s architecture. It’s the process of keeping track of the data and logic that make up your application. 

State management in Flutter is the process of managing the states of UI controls based on business logic requirements. The Provider package allows you to update the state in an efficient manner. 

When using Provider, widgets listen to changes in the state and update as soon as they are notified. This reduces the amount of work and makes the app run faster and more smoothly. 

Provider is a popular state management package in Flutter that allows you to manage the state of your app efficiently. Here’s a step-by-step guide on how to use Provider for state management in a Flutter app: 

1. Add Provider Dependency: 

Start by adding the provider package to your pubspec.yaml file: 

dependencies: 
  flutter: 
    sdk: flutter 
provider: ^6.0.3 

Fetch the latest version of the ‘provider’ package. 

Run ‘flutter pub get’ to fetch the package. 

2. Create a disposable provider class that extends ‘ChangeNotifier’: 

abstract class DisposableProvider with ChangeNotifier { 
} 

DisposableProvider is using the functionality provided by the ChangeNotifier class. 

The ChangeNotifier used to manage state changes and notify listeners of changes in the derived classes. 

Below class derived as how to use disposable provider class 

Class ProviderViewModel extends DisposableProvider { 
} 

3. Wrap Your App with ‘ChangeNotifierProvider’: 

‘ChangeNotifierProvider’ to provide access to the data model:

void main() { 
  runApp( 
    MultiProvider( 
      providers: [ 
        ChangeNotifierProvider(create: (context) => ProviderViewModel ()), 
      ], 
      child: const MyApp(), 
    ), 
  ); 
} 

void main(): This is the entry point of your Flutter application. The main() function is where your app is started. 

runApp(): This function is used to run your Flutter application, and it takes a single argument, which is the root widget of your application. 

MultiProvider: MultiProvider is a widget provided by the provider package. It’s used to manage multiple providers simultaneously. you can use it when multiple provider used in your app. 

providers: List of providers that you want to use in your app. In this code, you’re using two providers: 

ChangeNotifierProvider: This provider is used for state management. It’s used to create an instance of ProviderViewModel. The create parameter specifies a callback function that returns an instance of ProviderViewModel. This allows you to share the ProviderViewModel with widgets that need access to its state. 

child: const MyApp(): This line specifies the child widget of the MultiProvider. In above code, it’s a MyApp widget, which is the root widget of your application. 

4. Use provider with consumer 

return Consumer< providerModel >( 
  builder: (context, providerModel, child) { 
    return Text('Total price: ${providerModel.title}'); 
  }, 
); 

Above code is using the provider package in Flutter for state management, and it’s using the Consumer widget to rebuild a part of the user interface when the state in a providerModel changes. 
 

  • return Consumer<providerModel>(…): This line returns a Consumer widget. A Consumer widget observe to changes in a specified provider (in this case, providerModel) and rebuilds its child widget when the provider’s state changes. it helps to updating parts of the UI in response to changes in state. 
  • builder: (context, providerModel, child) { … }: The builder is a callback function that takes three parameters: 
  1. context: The BuildContext object, this object provides information about the location of the widget in the widget tree. 
  1. providerModel: An object of the providerModel class (or the provided data model) which is being observed by the Consumer.  
  1. child: A reference to the child passed to the Consumer. it is used when you want to include additional widgets in the widget tree but only rebuild a specific part of it in response to state changes. 

Another way to handle this providerViewModel 

late ProvierViewModel providerViewModel = ProvierViewModel (); 
providerViewModel = Provider.of< ProvierViewModel >(context, listen: false);

The listen: false parameter is used when you want to modify the model without triggering a rebuild of the widget listening to it. 

5. Dispose of the Model: 

It’s a good practice to dispose of the model when it’s no longer needed, such as when your widget is disposed:  

@override 
void disposeValues() {} 

6. Conclusion: 

In summary, diving into Flutter state management is key for crafting efficient apps. Provider, a popular package, smoothens state updates, boosting app performance. This guide offers a hands-on, step-by-step roadmap to effectively harness Provider for seamless state management in your Flutter projects.