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

on change function is being called on load #2

Closed
MasterNeuromancer opened this issue Jan 30, 2020 · 6 comments · Fixed by #6
Closed

on change function is being called on load #2

MasterNeuromancer opened this issue Jan 30, 2020 · 6 comments · Fixed by #6

Comments

@MasterNeuromancer
Copy link

when the widget loads, the state is false, and therefore unintended calls are being made on the app. Is there a way to isolate the on tap function only. I only want the calls to be made when a user taps the widget, not on load. Thank you.

@Bader-Al
Copy link

Bader-Al commented Jun 28, 2020

I had the same issue and found two solutions.. however, the second one destroyed the drag of a DraggableBottomSheet I have in my app.. So i stuck to the first one

1 - just wrap the onChanged function with WidgetsBinding as such :

WidgetsBinding.instance.addPostFrameCallback((_){

  // Add Your Code here.

});

2- the other method is to use SchedulerBinding :

SchedulerBinding.instance.addPostFrameCallback((_) {

  // add your code here.
});

@Bader-Al
Copy link

Incase you'd like to see where i found those solutions :
https://stackoverflow.com/questions/47592301/setstate-or-markneedsbuild-called-during-build

@MasterNeuromancer
Copy link
Author

Thanks @Bader-Al , I will look into that!

@rodrigobastosv
Copy link
Contributor

I just made a PR to solve this issue.

#6

@Huzaifa2019
Copy link

Thank you so much

@0ne0rZer0
Copy link

I was having the same problem what i did was add an if condition inside the _determine() function (as it is called during initState)
if (changeState) widget.onChanged(turnState);

@cgustav cgustav closed this as completed in #6 Sep 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants