Flutterexperts

Crafting Next-Gen Apps with Flutter Power
Can Stateful Builder Step into the shoes of Stateful Widget?

The answer is: Ahh A big Noo….

To understand this let’s first understand what Stateful Builder is?

StatefulBuilder is a widget having a mutable state (whose state can be change) what makes it’s special is its only rebuild the particular widget which is wrapped under the Stateful Builder. Thankfully no again and again rebuilding of the whole widget.No matter the widget which is wrapped under the Stateful Builder is in Stateless or stateful Widget.

Now question arising is we know Stateless Widget it can’t rebuild until it gets a new instance. ( SetState is not working)

what happens is Stateful builder has the power of StateSetter function passed to the builder is used to call a rebuild itself instead of a typical state of the whole Widget.

StatefulBuilder(
builder: (BuildContext context, StateSetter setState) => Checkbox(
value: isChecked,
onChanged: (value) {
setState(() {
isChecked = value;
});
},
),
),

KEEP IN MIND

Any variables that represent state should be kept outside the build function means You have to declare the variable outside the build method for the magic happens.

As I mentioned above it only rebuild a particular widget. It only gives a new instance(State) to that particular widget. so you have wrapped all widget(view) you want to see the change in StatefulBuilder.

In order to understand Why can’t Stateful Builder replace a Stateful widget. Lets first understand what makes is different between Stateless and Stateful Widget.

Have you noticed in stateless override build method and on the other hand in Stateful widget first override createState and then override build method? What happens under the hood is when stateless widget gets an instance its build function is invoked. and in stateful widget first, create state giving instance to build function. get it?? That’s how our favourite setState is working. After running setState ,createState giving a new instance to build method and widget rebuilds.

Now we are in pretty much good condition to understand Why can’t Stateful Builder replace Stateful widget.

If it’s rebuilding the particular part of the widget tree. Why we can’t use instead of Stateful Widget?

If you store any state in instance variables in the stateless widget it may be lost at any time as the constructor for the stateless widget may be called repeatedly, just like the build method. Only the associated instance State of a Stateful widget is preserved by Flutter.

StatefulWidget is retained it state even across rebuilds, That’s why you see sometimes misbehaviour of TextEditingController in Stateless Widget

Or if you on sturboness of using StatefulBuilder with Stateless Widget then you have to save state by Global keys.

Link for repository

flutter-devs/StatefulBuilderDemo
stateful builder This project is a starting point for a Flutter application. A few resources to get you started if this…github.com

Thanks for reading this article if you find anything that could be improved please let me know, I would love to improve.

Feel free to connect with us
And read more articles from FlutterDevs.com


FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! You can connect with us on Facebook, GitHub, and Twitter for any flutter related queries.

Leave comment

Your email address will not be published. Required fields are marked with *.