Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ChangeNotifier LifeCycle #799

Open
muhammadusamaijaz opened this issue Jan 9, 2023 · 6 comments
Open

ChangeNotifier LifeCycle #799

muhammadusamaijaz opened this issue Jan 9, 2023 · 6 comments
Assignees
Labels
enhancement New feature or request

Comments

@muhammadusamaijaz
Copy link

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:

  1. 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
   }
}
   
@muhammadusamaijaz muhammadusamaijaz added enhancement New feature or request needs triage labels Jan 9, 2023
@rrousselGit
Copy link
Owner

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?

@muhammadusamaijaz
Copy link
Author

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😊

@muhammadusamaijaz
Copy link
Author

after implementations there will be three callbacks in ChangeNotifier class

  1. onInit()
  2. onReady()
  3. dispose()

@rrousselGit
Copy link
Owner

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?

@renolation
Copy link

what you wrote just like getx ? my advice is stay away from changeNotifier. you only use it if others way didnt work

@rrousselGit rrousselGit self-assigned this May 10, 2023
@Liloupar
Copy link

Liloupar commented Jun 1, 2023

It feels very useful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants