jonataslaw / getx

Open screens/snackbars/dialogs/bottomSheets without context, manage states and inject dependencies easily with Get.
MIT License
10.34k stars 1.62k forks source link

`Get.previous` returns wrong route? #1870

Open fauzynurn opened 3 years ago

fauzynurn commented 3 years ago

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

hatemragab commented 2 years ago

same here ! and updates ?

felipecastrosales commented 1 year ago

any news about this?

kiloshell commented 1 year ago

can use system observer with navigatorObservers