provider
provider copied to clipboard
ChangeNotifier LifeCycle
Hi, I'm writing to let you know that I have an idea to create a lifecycle for ChangeNotifier
right now ChangeNotifier have only a single method called dispose(), whenever we need to execute a piece of code on model destruction we can put that code inside dispose() callback. but there are certain problems we face in real world apps that we do not have any callback of model initialization.
Real World Scenarios:
-
lets we have a screen [consider a SplashScreen], and we want that screen to show for sometime let say Timer(seconds: 5), now what we could write in our code? for now we have stateful widgets and we can utilize initState() callback, but there are some points in initState()
StatefulWidgets have alot of boilerplate code with many of other callbacks like, didChangeDependencies(), activate(), deactivate() etc, so just for initState() we're converting our stateless Widget into stateful which will ultimately affect the code by generating some boilerplate Secondly in initState(), if we have to execute any method from ChangeNotifier then we have to make the object of ChangeNotifier first like: Provider.of(context, listen: false).executeMethod(). And again here are two big ambiguities, one is that we cannot have context directly in our initState() callback (we have to addPostFramCallback()). Secondly, we're creating an object of ChangeNotifier separately and then executing a particular method that is again a hectic approach
Solution For these types of scenarios, I want to create some separate callbacks in ChangeNotifier like, onInit() and onReady(), this will be triggered just after ChangeNotifier is injected into the memory and we can configure our things inside these callbacks.
Code Snippets
class AbcClass with ChangeNotifier {
late TextEditingController controller;
@override
void onInit(){
contorller = TextEditingController();
}
@override
void onReady(){
// can perform some counter functions and notify things up
}
}
Hello again!
Could you expand on what those callbacks are supposed to do?
I'm also not sure what the issue with Timer as you mentioned is. Coulx you explain more about that problem?
Yes, @rrousselGit actually I just want to add replica of initState() in our ChangeNotifier, this callback is just like the one already added in ChangeNotifier i.e. dispose(). This callback will be responsible for notifying that, Hey your ChangeNotifier just added in the tree, do you want to perform any actions? And just like above mentioned callback (replica of initState) you can call it as onInit(), I'll create another callback i.e. onReady() which will be executed after postFrameCallback
So, what i want to mention is that ChangeNotifier class has dispose() method in it, I want to add onInit() and onReady() callback inside it which will be really helpful for all of us, because most of the time we need to execute a piece of code in initState() and just for initState() callback we need to convert our StatelessWidget into Stateful.
Stateful widget also have alot of boilerplate code as compared to Stateless, so if we create onInit() and onReady() callback in ChangeNotifier, then one can directly execute code inside our ChangeNotifier's onInit callback.
Let me know if its still confusing😊
after implementations there will be three callbacks in ChangeNotifier class
- onInit()
- onReady()
- dispose()
Could you not use the ChangeNotifier's constructor?
class Foo with ChangeNotifier {
Foo() {
// Perform initState-like logic
}
}
Also Provider
isn't responsible for ChangeNotifier
. I personally don't even recommend ChangeNotifier
.
What about proposing whatever change you have in mind to Flutter instead?
what you wrote just like getx ? my advice is stay away from changeNotifier. you only use it if others way didnt work
It feels very useful