Flutter State Management Without External Libraries.

Ricky Irfandi
2 min readMay 11, 2022

We know that there’re lots of great packages for managing states in flutter like bloc, provider, riverpod, getx, you name it. But, if you’re working on a quite simple app, you could try this approach:

Using Set State.

This is the basic state management in flutter. You can use it in the stateful widget by calling setState function. So after you write your code, you can call the setState then it will update the view. Or you can write your code inside the setState function.

Using Change Notifier

By using Change Notifier you only have to write setState once. First, you should extend your Controller class with ChangeNotifier, then at the function in controller call NotifyListener(). Don’t forget to add a listener in initState and fill it with the setState function.
Now every time notifyListener is called, it will trigger the listener to call setState.

try in dart pad :
https://dartpad.dev/?id=8eccc697d5fb5b71aa3a1bf4e2a377b6

But, what if we want our widget to remain stateless?

Using ValueListenableBuilder

You can use ValueNotifier variables in Controller, then, in our stateless widget, add ValueListenableBuilder. It will handle the state when the variables ValueNotifier change, and automatically update the view.

try in dart pad :
https://dartpad.dev/?id=14afd8ac46b05068570d08d107869cea

Using StreamBuilder

It’s like ValueListenableBuilder, but we use StreamController instead so we can utilize the advantages of the stream.

try in dart pad :
https://dartpad.dev/?id=3ca52bb28d4aa6db68d7597e0df08fb8

--

--