skip to Main Content

How can I know the list of previous pages in flutter, I’m using getx for navigation

I didn’t tried anything.
.

static void checkForRootLink() {

//get currenmt page of the app

log('Current route:${Get.currentRoute}');

log(RuntimeConfigs.routeObserver!.routeStack.toString());

// if (currentRoute == NamedRoutes.socialAppHomeIndex.path) {

//   //Already in home screen

//   //No need to do anything

// }

// // else if (Get.route) {

// //}

// else {

//   CustomRouting.replaceStackWithNamed(NamedRoutes.splashIndex.path);

//   // CustomRouting.popUntil(

//   //   NamedRoutes.root.path,

//   // );

// }

}

2

Answers


  1. Here is the solution

    Create a route observer file and paste the below code. Add the instance of route observer inside GetmaterailApp.

    import 'dart:developer';
    
    import 'package:flutter/material.dart';
    
    class RouteStackObserver extends NavigatorObserver {
      final List<String> routeStack = [];
    
      @override
      void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
        if (route.settings.name != null) {
          // Adding the route to the stack when a new route is pushed.
          log('RouteStack before push : $routeStack');
          routeStack.add(route.settings.name!);
          log('RouteStack after push : $routeStack');
        }
      }
    
      @override
      void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) {
        if (previousRoute != null && previousRoute.settings.name != null) {
          // Removing the last route from the stack when a route is popped.
          log('RouteStack before popped: $routeStack');
          routeStack.removeLast();
          log('RouteStack after popped: $routeStack');
        }
      }
    
      @override
      void didRemove(Route<dynamic> route, Route<dynamic>? previousRoute) {
        if (route.settings.name != null) {
          // Removing a specific route from the stack when it is removed.
          log('RouteStack before removed: $routeStack');
          routeStack.remove(route.settings.name);
          log('RouteStack after removed: $routeStack');
        }
      }
    
      @override
      void didReplace({Route<dynamic>? newRoute, Route<dynamic>? oldRoute}) {
        if (newRoute != null && newRoute.settings.name != null) {
          // Replacing the last route in the stack with the new route.
          log('RouteStack before replaced: $routeStack');
          if (routeStack.isNotEmpty) {
            routeStack.removeLast();
          }
          routeStack.add(newRoute.settings.name!);
          log('RouteStack after replaced: $routeStack');
        }
      }
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search