home_widget icon indicating copy to clipboard operation
home_widget copied to clipboard

call _checkForWidgetLaunch in afterInit not in didChangeDependencies

Open adrianvintu opened this issue 3 years ago • 4 comments

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 avatar Jan 24 '22 14:01 adrianvintu

@adrianvintu Thank you for this.

Jethro87 avatar Apr 08 '22 17:04 Jethro87

@Jethro87 you are very welcome. sorry for the not so intuitive description :)

adrianvintu avatar Apr 09 '22 05:04 adrianvintu

@adrianvintu would you mind to create a PR with this change to the example?

ABausG avatar Jun 05 '22 21:06 ABausG

sorry @ABausG, I am not familiar with how a PR works or what I should request there.

adrianvintu avatar Jun 06 '22 09:06 adrianvintu