Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter : addPostFrameCallback callback not called

Tags:

flutter

I try to implement a redirection system.

If I receive a notification (from Firebase messaging) when my application is in background, I store the sent route information (e.g: redirect = /medias/details/430) in shared preferences.

When the app opens, I check sharedPrefs (in the main) and if a redirect string is found, I add it to a stream:

MainSingleton().notificationStream.sink.add(redirectTo);

In the initState of my home page, I check if there is a redirect in the stream, and if it's the case, I redirect the user to the corect page with :

  void initState() {
    super.initState();

    MainSingleton().notificationStream.stream.listen((redirect) {
      if (redirect != null) {
        log("Home page found a redirect request: going to $redirect");
        WidgetsBinding.instance!.addPostFrameCallback((_) {
          log("Callback called");
          Application.router.navigateTo(context, redirect);
        });
      }
    });
   }

I got the message "Home page found a redirect request..." when I click the notification, but not the "Callback called", and obviously, not the redirection.

I need to set the app on background again, and resume it a second time to get the redirection.

WidgetsBinding.instance is not null, so I don't know why the callback is not called.

Am I wrong somewhere?

like image 436
Slot Avatar asked Jun 22 '26 09:06

Slot


1 Answers

I don’t know if it’s a good solution or only a workaround but you can use WidgetsBinding.instance!.ensureVisualUpdate to schedule a new frame, since probably your app is currently not generating any further frame and addPostFrameCallback gets never called.

MainSingleton().notificationStream.stream.listen((redirect) {
  if (redirect != null) {
    log(“Home page found a redirect request: going to $redirect”);
    WidgetsBinding.instance!.addPostFrameCallback((_) {
      log(“Callback called”);
      Application.router.navigateTo(context, redirect);
    });
  }
  WidgetsBinding.instance!.ensureVisualUpdate();
});
like image 93
Emanuele Avatar answered Jun 25 '26 01:06

Emanuele



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!