'Flutter: Custom Navigator ModalRoute.of does not work properly

I want to change my selected tabIndex when user uses back button.

So, I edited my willPopScope to get current Route name. I am using nested navigation. So my application has two navigators.

When I want to get current route name after pop action, It returning to me mainNavigatorKey's routeName.But I give it the coachNavigatorKey context..

Also I can not get the arguments from previous page data is always null.

ModalRoute.of(NavigatorKeys.coachNavigator.currentState!.context)!.settings.arguments;

WillPopScope(
  onWillPop: () async {
    await NavigatorKeys.coachNavigator.currentState!.maybePop();
    var s = ModalRoute.of(NavigatorKeys.coachNavigator.currentState!.context)!.settings.name;
    context.read<CoachMainCubit>().setTabIndexByName(s!);
    return false;
  },

Here is my Custom Navigator..

class CustomNavigator extends StatelessWidget {
  final GlobalKey<NavigatorState> navigatorKey;
  final List<RouteObserver>? routeObserverList;
  final Map<String, WidgetBuilder> routeList;

  const CustomNavigator({Key? key, required this.navigatorKey, required this.routeList, this.routeObserverList}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Navigator(
      key: navigatorKey,
      initialRoute: "/",
      observers: routeObserverList ?? [],
      onGenerateRoute: (settings) {
        return MaterialPageRoute(
          builder: (BuildContext context) => routeList[settings.name]!(context),
          settings: RouteSettings(name: settings.name),
        );
      },
    );
  }
}

Here is how I call my nested navigator:

class CoachMainView extends StatelessWidget {
  const CoachMainView({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        if (CoolAlertHelper().isLoadingDialogExists()) {
          return false;
        }
        await NavigatorKeys.coachNavigator.currentState!.maybePop();
        return false;
      },
      child: BlocProvider(
        create: (context) => CoachMainCubit(context),
        child: const CoachMainScreen(),
      ),
    );
  }
}

class CoachMainScreen extends StatelessWidget {
  const CoachMainScreen({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: buildCustomNavigator(context),
      bottomNavigationBar: buildBottomNavigationBar(context),
    );
  }

  Widget buildBottomNavigationBar(BuildContext context) {
    return SafeArea(
      child: Padding(
        padding: EdgeInsets.symmetric(horizontal: context.dynamicWidth(0.002), vertical: context.dynamicHeight(0.000)),
        child: BlocSelector<CoachMainCubit, CoachMainState, int>(
          selector: (state) {
            return context.read<CoachMainCubit>().currentTabIndex;
          },
          builder: (context, state) {
            return buildBottomNavBar(context);
          },
        ),
      ),
    );
  }

  CustomNavigator buildCustomNavigator(BuildContext context) {
    return CustomNavigator(
      navigatorKey: NavigatorKeys.coachNavigator,
      routeObserverList: [CoachRouteObserver(context)],
      routeList: CoachScreenConstants().getCoachRoutes(context),
    );
  }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source