getx icon indicating copy to clipboard operation
getx copied to clipboard

`Get.previous` returns wrong route?

Open fauzynurn opened this issue 3 years ago • 1 comments

I need to track every routing between pages with its previous page. I created my own observer class and passed it on routingCallback in GetMaterialApp. But i found that Get.previous returns wrong value. When i tried to dig through the Routing class, i found that the value.previous is assigned with wrong value.

@override
  void didPop(Route route, Route? previousRoute) {
    super.didPop(route, previousRoute);
    final currentRoute = _RouteData.ofRoute(route);
    final newRoute = _RouteData.ofRoute(previousRoute);

    if (currentRoute.isSnackbar) {
      // Get.log("CLOSE SNACKBAR ${currentRoute.name}");
      Get.log("CLOSE SNACKBAR");
    } else if (currentRoute.isBottomSheet || currentRoute.isDialog) {
      Get.log("CLOSE ${currentRoute.name}");
    } else if (currentRoute.isGetPageRoute) {
      Get.log("CLOSE TO ROUTE ${currentRoute.name}");
    }
    if (previousRoute != null) {
      RouterReportManager.reportCurrentRoute(previousRoute);
    }

    // Here we use a 'inverse didPush set', meaning that we use
    // previous route instead of 'route' because this is
    // a 'inverse push'
    _routeSend?.update((value) {
      // Only PageRoute is allowed to change current value
      if (previousRoute is PageRoute) {
        value.current = _extractRouteName(previousRoute) ?? '';
        value.previous = newRoute.name ?? ''; <-- isn't this supposed to be currentRoute.name?
      } else if (value.previous.isNotEmpty) {
        value.current = value.previous;
      }

      value.args = previousRoute?.settings.arguments;
      value.route = previousRoute;
      value.isBack = true;
      value.removed = '';
      value.isSnackbar = newRoute.isSnackbar;
      value.isBottomSheet = newRoute.isBottomSheet;
      value.isDialog = newRoute.isDialog;
    });

    // print('currentRoute.isDialog ${currentRoute.isDialog}');

    routing?.call(_routeSend);
  }

Let's say the app navigates from A to B, then go back to A. In this case, the previous page should be the B instead of A. Is the Get.previous supposed to return that value? or it is just me who misunderstand about using routingCallback?

Here's the observer class i made :

class Middleware {
  static observer(Routing routing) {
    switch (routing.current) {
      case FirstPage.routeName:
        debugPrint('first page tracker from ${routing.previous}');
        break;
      case SecondPage.routeName:
        debugPrint('second page tracker from ${routing.previous}');
        break;
    }
  }
}

Thanks!

To Reproduce Steps to reproduce the behavior:

  1. Create two pages
  2. Create an observer class
  3. Pass the observer function to routingCallback in GetMaterialApp
  4. The console show wrong Get.previous value

Expected behavior The Get.previous should return the previous page.

Screenshots If applicable, add screenshots to help explain your problem.

Flutter Version: Flutter (Channel stable, 2.2.3)

Getx Version: GetX: ^4.3.8

fauzynurn avatar Sep 19 '21 07:09 fauzynurn

same here ! and updates ?

hatemragab avatar Aug 16 '22 20:08 hatemragab

any news about this?

felipecastrosales avatar Mar 08 '23 00:03 felipecastrosales

can use system observer with navigatorObservers

kiloshell avatar Jun 16 '23 08:06 kiloshell