{"id":1054,"date":"2023-09-15T06:34:15","date_gmt":"2023-09-15T06:34:15","guid":{"rendered":"https:\/\/www.devopssupport.in\/blog\/?p=1054"},"modified":"2023-09-29T06:57:19","modified_gmt":"2023-09-29T06:57:19","slug":"state-management-in-flutter","status":"publish","type":"post","link":"https:\/\/www.devopssupport.in\/blog\/state-management-in-flutter\/","title":{"rendered":"State management in Flutter"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/1_kC8xgTsDVSoZNt5pDm9-Dw-1024x576.gif\" alt=\"\" class=\"wp-image-1056\" width=\"420\" height=\"236\" srcset=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/1_kC8xgTsDVSoZNt5pDm9-Dw-1024x576.gif 1024w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/1_kC8xgTsDVSoZNt5pDm9-Dw-300x169.gif 300w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/1_kC8xgTsDVSoZNt5pDm9-Dw-768x432.gif 768w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/1_kC8xgTsDVSoZNt5pDm9-Dw-1536x864.gif 1536w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/1_kC8xgTsDVSoZNt5pDm9-Dw-850x478.gif 850w\" sizes=\"auto, (max-width: 420px) 100vw, 420px\" \/><\/figure>\n\n\n\n<p>In\u00a0<strong>Flutter\u00a0<\/strong>everything is a\u00a0<strong>widget<\/strong>. A widget is\u00a0<strong>mostly\u00a0<\/strong>the Component on the Screen, like\u00a0<strong>Text<\/strong>,\u00a0<strong>Image<\/strong>, some\u00a0<strong>List,\u00a0<\/strong>or even screen. Below is the picture of some of the commonly used widgets<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-66-1024x576.png\" alt=\"\" class=\"wp-image-1057\" srcset=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-66-1024x576.png 1024w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-66-300x169.png 300w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-66-768x432.png 768w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-66-1536x864.png 1536w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-66-850x478.png 850w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-66.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Moreover, widget has its S<strong>tate<\/strong>\u00a0as well which is actually the\u00a0<strong>data\u00a0<\/strong>that is being used in building of widget, A text Widget may have String data in the state of to show that String on the screen.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-67-1024x576.png\" alt=\"\" class=\"wp-image-1058\" srcset=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-67-1024x576.png 1024w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-67-300x169.png 300w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-67-768x432.png 768w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-67-1536x864.png 1536w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-67-850x478.png 850w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-67.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Flutter uses the below equation\u00a0in fig(c)\u00a0to render the widget on the application screen. Where\u00a0f\u00a0is the build function of the widget, which defines how the widget would look like,\u00a0the state\u00a0is the\u00a0current state\u00a0of the widget, and\u00a0UI\u00a0is the actual output of the widget on the screen.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"288\" src=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-68-1024x288.png\" alt=\"\" class=\"wp-image-1059\" srcset=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-68-1024x288.png 1024w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-68-300x84.png 300w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-68-768x216.png 768w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-68-1536x432.png 1536w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-68-850x239.png 850w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-68.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>However, once you define the widget and it is built, then you can not change the widget appear change d). For instance, you can not change the String in Text widget dynamically, since\u00a0widgets are Immutable, i.e.,\u00a0Unchangeable.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-69-1024x576.png\" alt=\"\" class=\"wp-image-1060\" srcset=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-69-1024x576.png 1024w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-69-300x169.png 300w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-69-768x432.png 768w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-69-1536x864.png 1536w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-69-850x478.png 850w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-69.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Though widgets are\u00a0<strong>unchangeable\u00a0Fig<\/strong><em> (d)<\/em>, but widget\u2019s state are\u00a0mutable,\u00a0changeable\u00a0Fig <em>(e)<\/em>, these types of widget are called\u00a0<strong>Stateful widget<\/strong>,<strong>\u00a0<\/strong>and since Flutter is a\u00a0<strong>declarative Framework<\/strong>, whenever\u00a0<strong>the state of widget changes, the widget gets rendered again with the new state.\u00a0<\/strong>So changing the widget\u2019s state is indirectly same as changing the widget dynamically.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-70-1024x576.png\" alt=\"\" class=\"wp-image-1061\" srcset=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-70-1024x576.png 1024w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-70-300x169.png 300w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-70-768x432.png 768w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-70-1536x864.png 1536w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-70-850x478.png 850w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-70.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p id=\"0b05\">Flutter has two types of widgets\u00a0<strong>Stateful\u00a0<\/strong>widget, discussed above, and\u00a0<strong>Stateless\u00a0<\/strong>widget.<\/p>\n\n\n\n<p id=\"c0c5\"><strong>Stateful widget&nbsp;<\/strong>is the widget whose&nbsp;<strong>state is mutable<\/strong>, thus&nbsp;<strong>you can change the widget appearance<\/strong>&nbsp;<strong>dynamically&nbsp;<\/strong>by changing the state of widget. Changing the widget state&nbsp;<em>fig(f)<\/em>.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"f447\">setState()<\/h1>\n\n\n\n<p id=\"8595\">In Stateful widget, whenever there is some change that occurs in the state, we call setState() method, available inside the Stateful widget. Calling setState tells the Framework that the widget\u2019s state is updated, and the widget should be rebuilt, hence it is rebuilt with the new state, see Fig (f).<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-71-1024x576.png\" alt=\"\" class=\"wp-image-1062\" srcset=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-71-1024x576.png 1024w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-71-300x169.png 300w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-71-768x432.png 768w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-71-1536x864.png 1536w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-71-850x478.png 850w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-71.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Flutter uses the below equation to render widget, and if there is some change in the state of widget, then again it uses the same equation and the same build function, which defines how widget would look like, but with a new state value to draw updated UI on the screen.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-72-1024x576.png\" alt=\"\" class=\"wp-image-1063\" srcset=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-72-1024x576.png 1024w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-72-300x169.png 300w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-72-768x432.png 768w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-72-1536x864.png 1536w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-72-850x478.png 850w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-72.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p id=\"efcc\">On the other hand,&nbsp;<strong>Stateless<\/strong>&nbsp;widget is a widget whose<strong>&nbsp;state is immutable<\/strong>, thus&nbsp;<strong>you can not change the widget appearance dynamically&nbsp;<\/strong>in any way. So, they are used when we have static widget.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"6845\">State types.<\/h1>\n\n\n\n<p>Now if we make a Stateful widget, its state is only available to the widget itself. however, sometimes it is essential to have a&nbsp;common state between different widgets&nbsp;so that different widgets can use the same state data at the same time.<\/p>\n\n\n\n<p>Let\u2019s consider a short scenario. Say, if we are making&nbsp;a cart application&nbsp;in which a user can select products from the application and add them to the cart, and once He completes his shopping, he can navigate to the&nbsp;Checkout screen&nbsp;so that he can know and pay the bill.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-73-1024x576.png\" alt=\"\" class=\"wp-image-1064\" srcset=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-73-1024x576.png 1024w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-73-300x169.png 300w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-73-768x432.png 768w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-73-1536x864.png 1536w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-73-850x478.png 850w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-73.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Now as you can see&nbsp;in Fig (h), the cart, which is a list of selected products, is in the state of the Cart Screen widget, local to the widget. Now this&nbsp;Cart&nbsp;that is in the state of the Cart Screen widget is not available in the Checkout Screen widget, however, we need a Cart in the Checkout Screen as well for the calculation and making of bills.<\/p>\n\n\n\n<p>To solve this, we lift the state up to a widget which is the&nbsp;ancestor widget&nbsp;of all the widgets that are using that&nbsp;common state&nbsp;fig(i). This technique is called&nbsp;Lifting the state up.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-74-1024x576.png\" alt=\"\" class=\"wp-image-1065\" srcset=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-74-1024x576.png 1024w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-74-300x169.png 300w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-74-768x432.png 768w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-74-1536x864.png 1536w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-74-850x478.png 850w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-74.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>The State which is local to a widget or define in widget itself called\u00a0<strong>Ephemeral State\u00a0<\/strong>and the State that is common to different widgets is called\u00a0<strong>Global State\u00a0<\/strong>or\u00a0<strong>App State<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-75-1024x576.png\" alt=\"\" class=\"wp-image-1066\" srcset=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-75-1024x576.png 1024w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-75-300x169.png 300w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-75-768x432.png 768w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-75-1536x864.png 1536w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-75-850x478.png 850w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/09\/image-75.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p id=\"a2a0\">So,\u00a0<strong>State Management\u00a0<\/strong>is all about managing the state, deciding which widget\u2019s state needs to be mutable (<strong>Stateful widget<\/strong>), and which needs to be immutable<strong>\u00a0(Stateless Widget)<\/strong>.\u00a0<strong>Calling setState\u00a0<\/strong>whenever there is a change in the state of widget, and deciding where to put state either in widget itself or in the common ancestor widget.<\/p>\n\n\n\n<p id=\"1111\">That\u2019s the basics of\u00a0<strong>State Management\u00a0<\/strong>that you must learn and implement in order to master the art of\u00a0<strong>State Management,\u00a0<\/strong>because every advance state management like\u00a0<strong>Provider\u00a0<\/strong>,\u00a0<strong>BLoC<\/strong>, or\u00a0<strong>Redux<\/strong>, all implements, behind the scenes, the same thing, which we have discussed above.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In\u00a0Flutter\u00a0everything is a\u00a0widget. A widget is\u00a0mostly\u00a0the Component on the Screen, like\u00a0Text,\u00a0Image, some\u00a0List,\u00a0or even screen. Below is the picture of some of the commonly used widgets Moreover, widget&#8230; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1054","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1054","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/comments?post=1054"}],"version-history":[{"count":1,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1054\/revisions"}],"predecessor-version":[{"id":1067,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1054\/revisions\/1067"}],"wp:attachment":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/media?parent=1054"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/categories?post=1054"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/tags?post=1054"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}