callback function for navigation in Flutter

Issue

In Navigator, we can use .then.
for eg:

 Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) => XyzScreen(),
        ),
      ).then((value) => _someFuncion());

so is there any way to implement the same in go_router?

Solution

see https://github.com/flutter/flutter/issues/99663 and

It seems like as a main go_router functionality it is not supported yet. Mainly since it is incompatible with browsers otherwise. Support for fixing this is there, however in the project it has a somewhat low priority.

https://stackoverflow.com/a/73962826/4986655
here you can find a workaround, by registering a listener when the route changes.
So you could => ignore while on the other page (or even subroutes of that page), call the callback when back on this page + unregister the listener, and unregister the listener if otherwise or sth similar.

Answered By – superfuzzy

Answer Checked By – Timothy Miller (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *