Search code examples
flutternavigationflutter-go-router

Go_Router, How to get result in previous page


In my project i am setting up go_router navigation and it works great for navigate, but i found two issues in two cases which i am mentioning below.

  1. Not found a proper way to pass result in previous page

I have created a common function for routing in the whole app where i have defined all methods push, pop, remove. Now i need to setup a common method where i can pass some data into other screen and also need to get result in previos page when the second screen pop will going to be called.

static go(
  {required BuildContext context,
  required Mode mode,
  required String moveTo,
  var param}) async {
try {
  switch (mode) {
    case Mode.PUSH:
      context.push(RouteName.root + moveTo);
      break;
    case Mode.REPLACE:
      context.pushReplacement(RouteName.root + moveTo);
      break;
    case Mode.REMOVE:
      context.go(RouteName.root);
      context.pushReplacement(RouteName.root + moveTo);
      break;
    case Mode.POP:
      context.pop();
      break;
  }
} catch (e) {
  print(e);
}

}

  1. How to get page info during routing

In go_router i am unable to find a way where i can get the current page information if i pop the screen B to screen A. I need to track user activity so in this case i need to fetch the route info when user go or back into any screen. Please guide me if there is any way with go_router.


Solution

  • As far as I know there is currently no way to do this. But someone have made a fork repo & implemented it which you can use.

    Its on pub.dev with the name go_router_flow. It's exactly like the original go_router with this pop with value feature.

    Here is some example code:

    final bool? result = await context.push<bool>('/page2');
    
    WidgetsBinding.instance.addPostFrameCallback((_) {
      if(result){
        print('Page returned $result');
      }
    });
    

    You can read the discussion here