home_widget
home_widget copied to clipboard
call _checkForWidgetLaunch in afterInit not in didChangeDependencies
I have an app with 2 pages: Homepage and Quiz. When the user presses the widget on homescreen, the app opens with first screen Homepage and then the code from _checkForWidgetLaunch
void didChangeDependencies() {
Debug.d("Homepage didChangeDependencies");
super.didChangeDependencies();
//once the app is started from the widget, this is triggered ad infinitum
// _checkForWidgetLaunch();
// HomeWidget.widgetClicked.listen(_launchedFromWidget);
}
calls _launchedFromWidget
and navigates to Quiz (Navigator.push
).
When close Quiz/back button, the Homepage.didChangeDependencies() is called and the Quiz is stared again! This is an infinite cycle.
I managed to fix this by adding to Homepage.initState()
// //https://stackoverflow.com/questions/49466556/flutter-run-method-on-widget-build-complete
WidgetsBinding.instance.addPostFrameCallback((_) => _afterInit());
and in afterInit call the code
_afterInit() async {
_checkForWidgetLaunch();
HomeWidget.widgetClicked.listen(_launchedFromWidget);
}
I hope this helps other people as well. Maybe consider testing and updating documentation?
Thank you very much for your great effort, it's truly a wonderful and incredibly useful component.
@adrianvintu Thank you for this.
@Jethro87 you are very welcome. sorry for the not so intuitive description :)
@adrianvintu would you mind to create a PR with this change to the example?
sorry @ABausG, I am not familiar with how a PR works or what I should request there.