{"id":213,"date":"2024-01-01T09:00:25","date_gmt":"2024-01-01T09:00:25","guid":{"rendered":"https:\/\/200oksolutionssandbox.co.uk\/blog\/?p=213"},"modified":"2024-07-30T08:54:26","modified_gmt":"2024-07-30T08:54:26","slug":"how-to-use-provider-state-management-in-flutter","status":"publish","type":"post","link":"https:\/\/200oksolutionssandbox.co.uk\/blog\/how-to-use-provider-state-management-in-flutter\/","title":{"rendered":"How to use Provider State Management in Flutter"},"content":{"rendered":"\n<p>State management is a critical part of any user application&#8217;s architecture. It&#8217;s the process of keeping track of the data and logic that make up your application.&nbsp;<\/p>\n\n\n\n<p>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.&nbsp;<\/p>\n\n\n\n<p>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.&nbsp;<\/p>\n\n\n\n<p>Provider is a popular state management package in Flutter that allows you to manage the state of your app efficiently. Here&#8217;s a step-by-step guide on how to use Provider for state management in a Flutter app:&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Add Provider Dependency:&nbsp;<\/h2>\n\n\n\n<p>Start by adding the provider package to your <strong>pubspec.yaml<\/strong> file:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dependencies: \n\u202f flutter: \n\u202f\u202f\u202f sdk: flutter \nprovider: ^6.0.3 <\/code><\/pre>\n\n\n\n<p>Fetch the latest version of the <strong>&#8216;provider&#8217; <\/strong>package.&nbsp;<\/p>\n\n\n\n<p>Run <strong>&#8216;flutter pub get&#8217;<\/strong> to fetch the package.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Create a disposable provider class that extends &#8216;ChangeNotifier&#8217;:&nbsp;<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>abstract class DisposableProvider with ChangeNotifier { \n} <\/code><\/pre>\n\n\n\n<p>DisposableProvider is using the functionality provided by the ChangeNotifier class.&nbsp;<\/p>\n\n\n\n<p>The <strong>ChangeNotifier<\/strong> used to manage state changes and notify listeners of changes in the derived classes.&nbsp;<\/p>\n\n\n\n<p>Below class derived as how to use disposable provider class&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Class ProviderViewModel extends DisposableProvider { \n} <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3. Wrap Your App with &#8216;ChangeNotifierProvider&#8217;:&nbsp;<\/h2>\n\n\n\n<p>&#8216;ChangeNotifierProvider&#8217; to provide access to the data model:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() { \n\u202f runApp( \n\u202f\u202f\u202f MultiProvider( \n\u202f\u202f\u202f\u202f\u202f providers: &#91; \n\u202f\u202f\u202f\u202f\u202f\u202f\u202f ChangeNotifierProvider(create: (context) =&gt; ProviderViewModel ()), \n\u202f\u202f\u202f\u202f\u202f ], \n\u202f\u202f\u202f\u202f\u202f child: const MyApp(), \n\u202f\u202f\u202f ), \n\u202f ); \n} <\/code><\/pre>\n\n\n\n<p><strong>void main()<\/strong>: This is the entry point of your Flutter application. The <strong>main() <\/strong>function is where your app is started.&nbsp;<\/p>\n\n\n\n<p><strong>runApp():<\/strong> This function is used to run your Flutter application, and it takes a single argument, which is the root widget of your application.&nbsp;<\/p>\n\n\n\n<p><strong>MultiProvider:<\/strong> MultiProvider is a widget provided by the provider package. It&#8217;s used to manage multiple providers simultaneously. you can use it when multiple provider used in your app.&nbsp;<\/p>\n\n\n\n<p><strong>providers:<\/strong><strong> <\/strong>List of providers that you want to use in your app. In this code, you&#8217;re using two providers:&nbsp;<\/p>\n\n\n\n<p><strong>ChangeNotifierProvider<\/strong>: This provider is used for state management. It&#8217;s used to create an instance of <strong>ProviderViewModel<\/strong>. The create parameter specifies a callback function that returns an instance of <strong>ProviderViewModel<\/strong>. This allows you to share the <strong>ProviderViewModel <\/strong>with widgets that need access to its state.&nbsp;<\/p>\n\n\n\n<p><strong>child: const MyApp()<\/strong>: This line specifies the child widget of the <strong>MultiProvider<\/strong>. In above code, it&#8217;s a MyApp widget, which is the root widget of your application.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Use provider with consumer&nbsp;<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>return Consumer&lt; providerModel &gt;( \n\u202f builder: (context, providerModel, child) { \n\u202f\u202f\u202f return Text('Total price: ${providerModel.title}'); \n\u202f }, \n); <\/code><\/pre>\n\n\n\n<p>Above code is using the provider package in Flutter for state management, and it&#8217;s using the Consumer widget to rebuild a part of the user interface when the state in a providerModel changes.&nbsp;<br>&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>return Consumer&lt;providerModel&gt;(&#8230;)<\/strong>: This line returns a <strong>Consumer<\/strong> widget. A <strong>Consumer<\/strong> widget observe to changes in a specified provider (in this case, <strong>providerModel<\/strong>) and rebuilds its child widget when the provider&#8217;s state changes. it helps to updating parts of the UI in response to changes in state.&nbsp;<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>builder: (context, providerModel, child) { &#8230; }<\/strong>: The <strong>builder<\/strong> is a callback function that takes three parameters:&nbsp;<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\" start=\"1\">\n<li><strong>context<\/strong>: The <strong>BuildContext<\/strong> object, this object provides information about the location of the widget in the widget tree.&nbsp;<\/li>\n<\/ol>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li><strong>providerModel<\/strong>: An object of the <strong>providerModel<\/strong> class (or the provided data model) which is being observed by the <strong>Consumer<\/strong>.&nbsp;&nbsp;<\/li>\n<\/ol>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li><strong>child<\/strong>: A reference to the <strong>child<\/strong> passed to the <strong>Consumer<\/strong>. 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.&nbsp;<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Another way to handle this <strong>providerViewModel<\/strong>&nbsp;<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>late ProvierViewModel providerViewModel = ProvierViewModel (); \nproviderViewModel = Provider.of&lt; ProvierViewModel &gt;(context, listen: false);<\/code><\/pre>\n\n\n\n<p>The listen: false parameter is used when you want to modify the model without triggering a rebuild of the widget listening to it.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Dispose of the Model:&nbsp;<\/h2>\n\n\n\n<p>It&#8217;s a good practice to dispose of the model when it&#8217;s no longer needed, such as when your widget is disposed:<strong>\u00a0<\/strong>\u00a0<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@override \r\nvoid disposeValues() {} <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">6. Conclusion:\u00a0<\/h2>\n\n\n\n<p>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.\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>State management is a critical part of any user application&#8217;s architecture. It&#8217;s the process of&hellip;<\/p>\n","protected":false},"author":1,"featured_media":215,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,7],"tags":[9,12,13,10,11],"class_list":["post-213","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-flutter","category-mobile","tag-flutter","tag-flutter-development","tag-hybrid","tag-provider-state","tag-provider-state-management"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to use Provider State Management in Flutter<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/200oksolutionssandbox.co.uk\/blog\/how-to-use-provider-state-management-in-flutter\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to use Provider State Management in Flutter\" \/>\n<meta property=\"og:description\" content=\"State management is a critical part of any user application&#8217;s architecture. It&#8217;s the process of&hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/200oksolutionssandbox.co.uk\/blog\/how-to-use-provider-state-management-in-flutter\/\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/profile.php?id=61552217825863\" \/>\n<meta property=\"article:published_time\" content=\"2024-01-01T09:00:25+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-07-30T08:54:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/200oksolutionssandbox.co.uk\/blog\/wp-content\/uploads\/2024\/01\/provider-state-management-in-flutter-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"500\" \/>\n\t<meta property=\"og:image:height\" content=\"290\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"BlogAdmin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"BlogAdmin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to use Provider State Management in Flutter","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/200oksolutionssandbox.co.uk\/blog\/how-to-use-provider-state-management-in-flutter\/","og_locale":"en_US","og_type":"article","og_title":"How to use Provider State Management in Flutter","og_description":"State management is a critical part of any user application&#8217;s architecture. It&#8217;s the process of&hellip;","og_url":"https:\/\/200oksolutionssandbox.co.uk\/blog\/how-to-use-provider-state-management-in-flutter\/","article_publisher":"https:\/\/www.facebook.com\/profile.php?id=61552217825863","article_published_time":"2024-01-01T09:00:25+00:00","article_modified_time":"2024-07-30T08:54:26+00:00","og_image":[{"width":500,"height":290,"url":"https:\/\/200oksolutionssandbox.co.uk\/blog\/wp-content\/uploads\/2024\/01\/provider-state-management-in-flutter-1.png","type":"image\/png"}],"author":"BlogAdmin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"BlogAdmin","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/200oksolutionssandbox.co.uk\/blog\/how-to-use-provider-state-management-in-flutter\/#article","isPartOf":{"@id":"https:\/\/200oksolutionssandbox.co.uk\/blog\/how-to-use-provider-state-management-in-flutter\/"},"author":{"name":"BlogAdmin","@id":"https:\/\/200oksolutionssandbox.co.uk\/blog\/#\/schema\/person\/ea0b72006227b3403f5ad825b82ced43"},"headline":"How to use Provider State Management in Flutter","datePublished":"2024-01-01T09:00:25+00:00","dateModified":"2024-07-30T08:54:26+00:00","mainEntityOfPage":{"@id":"https:\/\/200oksolutionssandbox.co.uk\/blog\/how-to-use-provider-state-management-in-flutter\/"},"wordCount":698,"commentCount":0,"publisher":{"@id":"https:\/\/200oksolutionssandbox.co.uk\/blog\/#organization"},"image":{"@id":"https:\/\/200oksolutionssandbox.co.uk\/blog\/how-to-use-provider-state-management-in-flutter\/#primaryimage"},"thumbnailUrl":"https:\/\/200oksolutionssandbox.co.uk\/blog\/wp-content\/uploads\/2024\/01\/provider-state-management-in-flutter-1.png","keywords":["flutter","flutter development","hybrid","provider state","provider state management"],"articleSection":["Flutter","Mobile"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/200oksolutionssandbox.co.uk\/blog\/how-to-use-provider-state-management-in-flutter\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/200oksolutionssandbox.co.uk\/blog\/how-to-use-provider-state-management-in-flutter\/","url":"https:\/\/200oksolutionssandbox.co.uk\/blog\/how-to-use-provider-state-management-in-flutter\/","name":"How to use Provider State Management in Flutter","isPartOf":{"@id":"https:\/\/200oksolutionssandbox.co.uk\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/200oksolutionssandbox.co.uk\/blog\/how-to-use-provider-state-management-in-flutter\/#primaryimage"},"image":{"@id":"https:\/\/200oksolutionssandbox.co.uk\/blog\/how-to-use-provider-state-management-in-flutter\/#primaryimage"},"thumbnailUrl":"https:\/\/200oksolutionssandbox.co.uk\/blog\/wp-content\/uploads\/2024\/01\/provider-state-management-in-flutter-1.png","datePublished":"2024-01-01T09:00:25+00:00","dateModified":"2024-07-30T08:54:26+00:00","breadcrumb":{"@id":"https:\/\/200oksolutionssandbox.co.uk\/blog\/how-to-use-provider-state-management-in-flutter\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/200oksolutionssandbox.co.uk\/blog\/how-to-use-provider-state-management-in-flutter\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/200oksolutionssandbox.co.uk\/blog\/how-to-use-provider-state-management-in-flutter\/#primaryimage","url":"https:\/\/200oksolutionssandbox.co.uk\/blog\/wp-content\/uploads\/2024\/01\/provider-state-management-in-flutter-1.png","contentUrl":"https:\/\/200oksolutionssandbox.co.uk\/blog\/wp-content\/uploads\/2024\/01\/provider-state-management-in-flutter-1.png","width":500,"height":290,"caption":"provider-state-management-in-flutter"},{"@type":"BreadcrumbList","@id":"https:\/\/200oksolutionssandbox.co.uk\/blog\/how-to-use-provider-state-management-in-flutter\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/200oksolutionssandbox.co.uk\/blog\/"},{"@type":"ListItem","position":2,"name":"How to use Provider State Management in Flutter"}]},{"@type":"WebSite","@id":"https:\/\/200oksolutionssandbox.co.uk\/blog\/#website","url":"https:\/\/200oksolutionssandbox.co.uk\/blog\/","name":"","description":"","publisher":{"@id":"https:\/\/200oksolutionssandbox.co.uk\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/200oksolutionssandbox.co.uk\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/200oksolutionssandbox.co.uk\/blog\/#organization","name":"Web Development Blog | Software Blog | App Blog","url":"https:\/\/200oksolutionssandbox.co.uk\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/200oksolutionssandbox.co.uk\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/200oksolutionssandbox.co.uk\/blog\/wp-content\/uploads\/2026\/01\/200ok_logo.png","contentUrl":"https:\/\/200oksolutionssandbox.co.uk\/blog\/wp-content\/uploads\/2026\/01\/200ok_logo.png","width":500,"height":191,"caption":"Web Development Blog | Software Blog | App Blog"},"image":{"@id":"https:\/\/200oksolutionssandbox.co.uk\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/profile.php?id=61552217825863"]},{"@type":"Person","@id":"https:\/\/200oksolutionssandbox.co.uk\/blog\/#\/schema\/person\/ea0b72006227b3403f5ad825b82ced43","name":"BlogAdmin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/200oksolutionssandbox.co.uk\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/53ecc99d859b4d3444ee1e076f3b5d9da9962836d1c20b3b44d73574f435740d?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/53ecc99d859b4d3444ee1e076f3b5d9da9962836d1c20b3b44d73574f435740d?s=96&d=mm&r=g","caption":"BlogAdmin"},"sameAs":["http:\/\/blog.200oksolutions.com"],"url":"https:\/\/200oksolutionssandbox.co.uk\/blog\/author\/blogadmin\/"}]}},"_links":{"self":[{"href":"https:\/\/200oksolutionssandbox.co.uk\/blog\/wp-json\/wp\/v2\/posts\/213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/200oksolutionssandbox.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/200oksolutionssandbox.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/200oksolutionssandbox.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/200oksolutionssandbox.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=213"}],"version-history":[{"count":2,"href":"https:\/\/200oksolutionssandbox.co.uk\/blog\/wp-json\/wp\/v2\/posts\/213\/revisions"}],"predecessor-version":[{"id":216,"href":"https:\/\/200oksolutionssandbox.co.uk\/blog\/wp-json\/wp\/v2\/posts\/213\/revisions\/216"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/200oksolutionssandbox.co.uk\/blog\/wp-json\/wp\/v2\/media\/215"}],"wp:attachment":[{"href":"https:\/\/200oksolutionssandbox.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/200oksolutionssandbox.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/200oksolutionssandbox.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}