<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jenish Hirpara on Medium]]></title>
        <description><![CDATA[Stories by Jenish Hirpara on Medium]]></description>
        <link>https://medium.com/@jenishhirpara23?source=rss-358d3c625284------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*7lHQHca9rOd7JOb8</url>
            <title>Stories by Jenish Hirpara on Medium</title>
            <link>https://medium.com/@jenishhirpara23?source=rss-358d3c625284------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 09:20:06 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jenishhirpara23/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[State Management using Providers in Flutter]]></title>
            <link>https://medium.com/@jenishhirpara23/state-management-using-providers-in-flutter-2644a7bf1132?source=rss-358d3c625284------2</link>
            <guid isPermaLink="false">https://medium.com/p/2644a7bf1132</guid>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[app-development]]></category>
            <category><![CDATA[state-management]]></category>
            <category><![CDATA[provider]]></category>
            <dc:creator><![CDATA[Jenish Hirpara]]></dc:creator>
            <pubDate>Sat, 04 Dec 2021 07:28:38 GMT</pubDate>
            <atom:updated>2021-12-04T07:28:38.114Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h4AF5qVpU24WR9xGONXxjA.jpeg" /></figure><h3><strong>Prerequisites</strong></h3><p>Before reading this article, you must have a basic understanding of what state is in flutter. If you are not aware about state, or want to know more, I recommend you first read these articles.</p><ul><li><a href="https://medium.com/codechai/basics-of-state-management-in-flutter-497ddb506608">Basics of State Management in Flutter.</a></li><li><a href="https://medium.com/dhiwise/7-things-you-need-to-know-about-flutter-state-management-42f840ef022e">7 things you need to know about Flutter State Management</a></li></ul><h3><strong>Let’s Get Started</strong></h3><p>To give you a gist, state is any kind of data that is generally accessible from anywhere in the app and can be changed globally so that every screen using that data is updated accordingly.</p><p>There are various packages in <a href="https://pub.dev/">https://pub.dev/</a> which are used for state management, but if you are a beginner in flutter, then I suggest you to use Provider. Let’s get started —</p><p>Go to the below link and add the latest version of provider as a dependency in your pubspec.yaml file.</p><p><a href="https://pub.dev/packages/provider/install">provider | Flutter Package</a></p><h3>Implementation</h3><p>Let us go through a simple implementation of managing state. Let us look at the both the codes</p><ul><li>Without Provider</li><li>With Provider</li></ul><p>This will give a better idea how important it is to use Provider, especially when the app is of a larger scale.</p><p>This is the app that we will build, a button on Page1 should update the value on Page2 dynamically.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fplayer.vimeo.com%2Fvideo%2F648030353%3Fh%3Dcecab3b830%26app_id%3D122963&amp;dntp=1&amp;display_name=Vimeo&amp;url=https%3A%2F%2Fvimeo.com%2F648030353&amp;image=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F1305222033-58af3c42ad7e6acb6be932641c96531174f38c199ba808a16_960&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=vimeo" width="970" height="2048" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c245f44b690d9321b5b3fc62fb8d73c/href">https://medium.com/media/3c245f44b690d9321b5b3fc62fb8d73c/href</a></iframe><h4>Without Providers</h4><p><em>page1.dart (without Provider):</em></p><pre>import &#39;package:flutter/material.dart&#39;;</pre><pre>import &#39;./page2.dart&#39;;</pre><pre>List&lt;String&gt; text = [</pre><pre>&quot;Providers are awesome&quot;,</pre><pre>&quot;Flutter is cool&quot;,</pre><pre>&quot;Flutter with providers is super cool&quot;,</pre><pre>];</pre><pre>int index = 0;</pre><pre>class Page1 extends StatefulWidget {</pre><pre>static const routeName = &#39;page1&#39;;</pre><pre>const Page1({Key? key}) : super(key: key);<br></pre><pre>@override</pre><pre>_Page1State createState() =&gt; _Page1State();</pre><pre>}</pre><pre>class _Page1State extends State&lt;Page1&gt; {</pre><pre>void changeText() {</pre><pre>if (index == text.length - 1) {</pre><pre>index = 0;</pre><pre>} else {</pre><pre>index++;</pre><pre>}</pre><pre>}<br></pre><pre>@override</pre><pre>Widget build(BuildContext context) {</pre><pre>return Scaffold(</pre><pre>appBar: AppBar(</pre><pre>title: const Text(&#39;Page1&#39;),</pre><pre>),</pre><pre>body: Center(</pre><pre>child: Row(</pre><pre>mainAxisAlignment: MainAxisAlignment.spaceAround,</pre><pre>crossAxisAlignment: CrossAxisAlignment.center,</pre><pre>children: [</pre><pre>ElevatedButton(</pre><pre>onPressed: changeText,</pre><pre>child: const Text(&#39;Change Text&#39;),</pre><pre>),</pre><pre>ElevatedButton(</pre><pre>onPressed: () {</pre><pre>Navigator.of(context).pushNamed(Page2.routeName,</pre><pre>arguments: {&#39;text&#39;: text[index]});</pre><pre>},</pre><pre>child: const Text(&#39;Goto Page 2&#39;),</pre><pre>),</pre><pre>],</pre><pre>),</pre><pre>),</pre><pre>);</pre><pre>}</pre><pre>}</pre><p><em>page2.dart (without Provider):</em></p><pre>import &#39;package:flutter/material.dart&#39;;</pre><pre>class Page2 extends StatelessWidget {</pre><pre>static const routeName = &#39;page2&#39;;</pre><pre>const Page2({Key? key}) : super(key: key);</pre><pre>@override</pre><pre>Widget build(BuildContext context) {</pre><pre>var text =</pre><pre>ModalRoute.of(context)!.settings.arguments as Map&lt;String, String&gt;;</pre><pre>return Scaffold(</pre><pre>appBar: AppBar(</pre><pre>title: const Text(&#39;Page2&#39;),</pre><pre>),</pre><pre>body: Center(</pre><pre>child: Text(</pre><pre>text[&#39;text&#39;]!,</pre><pre>style: const TextStyle(fontSize: 20),</pre><pre>)),</pre><pre>);</pre><pre>}</pre><pre>}</pre><p>Page1 contains two buttons:</p><ul><li>To change text in the second page</li><li>To navigate to the second page.</li></ul><p>There is a global variable index declared in Page1 file which is used for iterating through the list of texts. Without provider, the List of String along with the index has to be passed as an argument or a parameter to Page2. This becomes confusing especially when there are a large number of variables which are used in different screens.</p><p>Provider solves this problem by making a global class to define all the variables to be used globally. If a particular screen needs to use a variable in the provider, it just needs to call the appropriate provider and update the value accordingly.</p><h4>With Providers</h4><p><em>text_provider.dart:</em></p><pre>import &#39;package:flutter/material.dart&#39;;<br></pre><pre>class TextProvider with ChangeNotifier {</pre><pre>final List&lt;String&gt; _text = [</pre><pre>&quot;Providers are awesome&quot;,</pre><pre>&quot;Flutter is cool&quot;,</pre><pre>&quot;Flutter with providers is super cool&quot;,</pre><pre>];</pre><pre>int _index = 0;</pre><pre>int get index {</pre><pre>return _index;</pre><pre>}</pre><pre>List&lt;String&gt; get text {</pre><pre>return [..._text];</pre><pre>}</pre><pre>void changeText() {</pre><pre>if (index == text.length - 1) {</pre><pre>_index = 0;</pre><pre>} else {</pre><pre>_index++;</pre><pre>}<br>notifyListeners();</pre><pre>}</pre><pre>}</pre><p>Do not forget to use ‘with ChangeNotifier’.</p><ul><li>ChangeNotifier — It simply allows the classes using the provider to listen to any changes in the state.</li><li>notifyListeners — Calls all the listeners when the state changes so that they are dynamically updated.</li></ul><p>Also, keep in mind that you can not access the variables inside the TextProvider class directly from any file. You need to create getter classes for those variables and call them instead.</p><p>Also before using the Provider inside Page1 and Page2, we need to wrap the MaterialApp class in main.dart file with Provider.</p><p><em>main.dart (with Provider):</em></p><pre>@override</pre><pre>Widget build(BuildContext context) {</pre><pre>return ChangeNotifierProvider(</pre><pre>create: (ctx) =&gt; TextProvider(),</pre><pre>child: MaterialApp(</pre><pre>debugShowCheckedModeBanner: false,</pre><pre>title: &#39;Flutter Demo&#39;,</pre><pre>theme: ThemeData(</pre><pre>primarySwatch: Colors.blue,</pre><pre>),</pre><pre>home: const Page1(),</pre><pre>routes: {</pre><pre>Page1.routeName: (ctx) =&gt; const Page1(),</pre><pre>Page2.routeName: (ctx) =&gt; const Page2(),</pre><pre>},</pre><pre>),</pre><pre>);</pre><pre>}</pre><p><em>page1.dart (with Provider):</em></p><pre>import &#39;package:flutter/material.dart&#39;;</pre><pre>import &#39;./page2.dart&#39;;</pre><pre>import &#39;text_provider.dart&#39;;</pre><pre>import &#39;package:provider/provider.dart&#39;;</pre><pre>class Page1 extends StatefulWidget {</pre><pre>static const routeName = &#39;page1&#39;;</pre><pre>const Page1({Key? key}) : super(key: key);</pre><pre>@override</pre><pre>_Page1State createState() =&gt; _Page1State();</pre><pre>}</pre><pre>class _Page1State extends State&lt;Page1&gt; {</pre><pre>@override</pre><pre>Widget build(BuildContext context) {</pre><pre>var text = Provider.of&lt;TextProvider&gt;(context);</pre><pre>return Scaffold(</pre><pre>appBar: AppBar(</pre><pre>title: const Text(&#39;Page1&#39;),</pre><pre>),</pre><pre>body: Center(</pre><pre>child: Row(</pre><pre>mainAxisAlignment: MainAxisAlignment.spaceAround,</pre><pre>crossAxisAlignment: CrossAxisAlignment.center,</pre><pre>children: [</pre><pre>ElevatedButton(</pre><pre>onPressed: text.changeText,</pre><pre>child: const Text(&#39;Change Text&#39;),</pre><pre>),</pre><pre>ElevatedButton(</pre><pre>onPressed: () {</pre><pre>Navigator.of(context).pushNamed(Page2.routeName);</pre><pre>},</pre><pre>child: const Text(&#39;Goto Page 2&#39;),</pre><pre>),</pre><pre>],</pre><pre>),</pre><pre>),</pre><pre>);</pre><pre>}</pre><pre>}</pre><p><em>page2.dart (with Provider):</em></p><pre>import &#39;package:flutter/material.dart&#39;;</pre><pre>import &#39;package:provider/provider.dart&#39;;</pre><pre>import &#39;./text_provider.dart&#39;;</pre><pre>class Page2 extends StatelessWidget {</pre><pre>static const routeName = &#39;page2&#39;;</pre><pre>const Page2({Key? key}) : super(key: key);</pre><pre>@override</pre><pre>Widget build(BuildContext context) {</pre><pre>var text = Provider.of&lt;TextProvider&gt;(context);</pre><pre>return Scaffold(</pre><pre>appBar: AppBar(</pre><pre>title: const Text(&#39;Page2&#39;),</pre><pre>),</pre><pre>body: Center(</pre><pre>child: Text(</pre><pre>text.text[text.index],</pre><pre>style: const TextStyle(fontSize: 20),</pre><pre>)),</pre><pre>);</pre><pre>}</pre><pre>}</pre><h3>Different Kinds of Providers</h3><p>If you noticed, in this example we used only one Provider, which was the TextProvider class, in cases where you need multiple providers, you will need to use MultiProvider class in main.dart file</p><pre>MultiProvider(<br>  providers: [<br>    ChangeNotifierProvider&lt;Something&gt;(create: (_) =&gt; Something()),<br>    ChangeNotifierProvider&lt;SomethingElse&gt;(create: (_) =&gt; SomethingElse()),<br>    ChangeNotifierProvider&lt;AnotherThing&gt;(create: (_) =&gt; AnotherThing()),<br>  ],<br>  child: someWidget,<br>)</pre><p><a href="https://pub.dev/documentation/provider/latest/provider/MultiProvider-class.html">MultiProvider class</a></p><p>In case the value from one provider is to used in some other provider, then ChangeNotifierProxyProvider is used. Take a look below:</p><p><a href="https://pub.dev/documentation/provider/latest/provider/ChangeNotifierProxyProvider-class.html">ChangeNotifierProxyProvider class - provider library - Dart API</a></p><h3>Summary</h3><p>Provider is used to</p><ul><li>Manage state in Flutter globally.</li><li>It allows us to get and manipulate data from any file.</li><li>Can also be used to call APIs and manipulate the data accordingly.</li><li>Help to compact the code, making it easier to understand and store data.</li></ul><p>My github repo for the code above:</p><p><a href="https://github.com/JenishHirpara/State_management_Flutter">GitHub - JenishHirpara/State_management_Flutter</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2644a7bf1132" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>