<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Solomon Nsumei</title>
    <description>The latest articles on DEV Community by Solomon Nsumei (@solnsumei).</description>
    <link>https://dev.to/solnsumei</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F611639%2F34b21e27-ce71-48aa-9585-7cac8cbc0344.jpeg</url>
      <title>DEV Community: Solomon Nsumei</title>
      <link>https://dev.to/solnsumei</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/solnsumei"/>
    <language>en</language>
    <item>
      <title>Easy State Management with ValueNotifiers and Inherited Widgets in Flutter</title>
      <dc:creator>Solomon Nsumei</dc:creator>
      <pubDate>Wed, 26 May 2021 14:27:47 +0000</pubDate>
      <link>https://dev.to/solnsumei/easy-state-management-with-valuenotifiers-and-inherited-widgets-in-flutter-1070</link>
      <guid>https://dev.to/solnsumei/easy-state-management-with-valuenotifiers-and-inherited-widgets-in-flutter-1070</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;The flutter ecosystem has grown rapidly in a few years, seeing it grow from just a mobile development framework to support both web and desktop development.&lt;/p&gt;

&lt;p&gt;Building mobile apps using flutter, there are tons of state management libraries or packages to choose from which can be a beginner's nightmare.&lt;/p&gt;

&lt;p&gt;In this tutorials we will be managing our own app state with only InheritedWidgets, ValueNotifiers and ValueListenableBuilders.&lt;/p&gt;

&lt;h2&gt;
  
  
  What we will be building
&lt;/h2&gt;

&lt;p&gt;We will be implementing a favorites list from a list of fruits.&lt;/p&gt;

&lt;h4&gt;
  
  
  At the end of this tutorial you should
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;have a good understanding of InheritedWidgets, ValueNotifiers and ValueListenableBuilder and how they can be used to manage state in your flutter applications.&lt;/li&gt;
&lt;li&gt;be able to use them to manage you own application state without needing StatefulWidgets or external state management libraries or packages.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So let's dive right in...&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating your flutter project
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Create a new flutter project. I will not go into details of creating a flutter project since it is pretty straightforward.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is a look of our boilerplate after cleanup&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now lets take a look at the ValueNotifier with the ValueListenableBuilder and how they can help us manage state in our flutter apps.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementing a Simple ValueNotifier
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;ValueNotifier&lt;/strong&gt; class is a subclass of ChangeNotifier which manages the state of a single value and notifies listeners when the value changes. It works with the &lt;strong&gt;ValueListenableBuilder&lt;/strong&gt; widget which automatically listens to changes on the valueListenable and rebuilds enclosed widgets accordingly.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;If you run the code above you discover that the counter value changes but the app does not display the changes. This is where the ValueListenableBuilder comes in.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ValueListenableBuilder&lt;/strong&gt; is a widget whose content is always  synced with a ValueListenable. The ValueListenableBuilder requires two arguments, a ValueListenable, which is a ValueNotifier instance, and a builder, which is a callback that rebuilds the widget based on the changes to the value being listened to. Implementing that in our code will look like&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now that you know what a valueNotifier and valueListenableBuilder are and how to use them, let's get started on our project.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementing a Custom ValueNotifier
&lt;/h3&gt;

&lt;p&gt;The above example showed a simple use case of the ValueNotifier, for more complex and custom implementation, one will need to extend the ValueNotifier class.&lt;br&gt;
Now, lets create our custom ValueNotifier class&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Because we are interested in a list of favorites, we made our &lt;strong&gt;FavoriteNotifier&lt;/strong&gt; class inherit from ValueNotifier&amp;gt; and implemented the constructor.&lt;/p&gt;

&lt;p&gt;Next we add methods to add and remove from the favorites list.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Notice the use of the &lt;strong&gt;notifyListeners&lt;/strong&gt; method in each method we implemented. Without it the listeners will not get notified when the value changes or is updated.&lt;/p&gt;

&lt;p&gt;There's a problem, how do we pass our custom valueNotifier down our widget tree to maintain the same instance and not multiple instances? Enter &lt;strong&gt;InheritedWidgets&lt;/strong&gt;, but before then lets update our UI code.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We have completed our UI and included a FavoriteCount widget to show how many items are in our favorites list, as well as a list of fruits to our app page.&lt;/p&gt;

&lt;p&gt;Running the app now will display a list but clicking on any of the items will do nothing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Wrapping it all with the Inherited Widget
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;InheritedWidgets&lt;/strong&gt; allow us to receive data from widget ancestors without having to pass it down a widget tree.&lt;/p&gt;

&lt;p&gt;We can create our own Inherited widget by extending the InheritedWidget class, adding the data we want to share, and creating an &lt;code&gt;of&lt;/code&gt; method to call in our child widgets whenever we want to access the data.&lt;/p&gt;

&lt;p&gt;Let's implement that in our code.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now lets wrap our MaterialsApp with the FavoriteState so the data it holds will be available in the child widgets also.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Finally we will be wrapping the children we need to receive updates from our FavoriteNotifier with ValueListenableBuilder and implement calls to update the UI accordingly.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Running the app now will show the fruits in the favorites list icons as gold, while the others as grey, also the FAB button clears the list.&lt;/p&gt;

&lt;p&gt;Now let's update the FavoriteCount widget to display the count of fruits in our favorite collection.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;finally, if you run the app now, you notice that both the icon color and the favorite count is updated when we tap on any of the fruit.&lt;/p&gt;

&lt;p&gt;Here is the full code:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You can run the final code on dartpad by clicking this link&lt;br&gt;
&lt;a href="https://dartpad.dev/23105c6c1a7002593771912352e613cf"&gt;Code Editor&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;We simply constructed our own application state with flutter's built-in widgets, without the need for external libraries or rebuilding the entire widget tree with setState in a StatefulWidget.&lt;/p&gt;

&lt;p&gt;You can explore more on your own and post your comments. If you have any question, please post it on the comments section and I'll be glad to answer. Happy coding!&lt;/p&gt;

</description>
      <category>flutter</category>
      <category>mobiledev</category>
      <category>programming</category>
      <category>dart</category>
    </item>
  </channel>
</rss>
