Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the WidgetsBindingObserver work on a stateless widget?

Tags:

flutter

dart

I am trying to use the WidgetsBindingObserver to see if my app is brought to the foreground. But it doesn't seem do anything. Does it only work on a statefull widget?

class TheHomeView extends StatelessWidget with WidgetsBindingObserver {

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    print('lifecycle changed');
    if (state == AppLifecycleState.resumed) {
      print('resumed');
      showLatestGroupNotification();
    }
  }
like image 768
anonymous-dev Avatar asked Jun 30 '26 06:06

anonymous-dev


1 Answers

Well it turns out, you can use it in a stateless widget. But you need to use

WidgetsBinding.instance.addObserver(this);

which you can do in the constructor of the widget. But if you want to remove the binding on dispose

WidgetsBinding.instance.removeObserver(this);

You would need a dispose which is only available in a statefull widget. Or you would have to do it manually.

https://dev.to/pedromassango/onresume-and-onpause-for-widgets-on-flutter-27k2

like image 94
anonymous-dev Avatar answered Jul 02 '26 20:07

anonymous-dev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!