Q: Flutter: "Multiple GlobalKeys in the Widget tree" while Navigation I'm facing an issue of "Multiple GlobalKeys in the Widget tree" while Navigation.
I have a BottomNavigationBar & a Drawer defined in the Scaffold of a Base Screen and in the body parameter of Scaffold I have multiple screens which I'm accessing with BottomNavigationBar. The thing is, I'm accessing the Drawer of the Base Screen from one of the multiple screens by using a GlobalKey, and everything's working fine but when I Navigate to the Base Screen from Another Screen then I get the above-mentioned error.
I have tried a solution of not using a static keyword while defining the key and it solves the error of navigation but then I can't access the Drawer because then I get another error of "method 'openDrawer' was called on null".
This is a separate class where I have defined the Key:

class AppKeys {
  final GlobalKey<ScaffoldState> homeKey = GlobalKey<ScaffoldState>();
}

This is the Base Screen:
class Base extends StatefulWidget {
  @override
  _BaseState createState() => _BaseState();
}

class _BaseState extends State<Base> {
  int selectedScreen = 0;
  final screens = List<Widget>.unmodifiable([Home(), Cart(), Orders(), Help()]);

  AppKeys appKeys = AppKeys();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer: MyDrawer(),
      key: appKeys.homeKey,
      body: screens[selectedScreen],
      bottomNavigationBar: SizedBox(
        height: 80,
        child: BottomNavigationBar(
          onTap: (val) {
            setState(() {
              selectedScreen = val;
            });
          },
          currentIndex: selectedScreen,
          selectedItemColor: AppColor.primary,
          elevation: 20.0,
          unselectedItemColor: Colors.grey,
          showUnselectedLabels: true,
          type: BottomNavigationBarType.fixed,
          iconSize: 25,
          selectedFontSize: 15,
          unselectedFontSize: 15,
          items: [
            BottomNavigationBarItem(
                icon: Icon(AnanasIcons.home), title: Text("Home")),
            BottomNavigationBarItem(
                icon: Icon(AnanasIcons.cart), title: Text("Cart")),
            BottomNavigationBarItem(
                icon: Icon(AnanasIcons.orders), title: Text("My Orders")),
            BottomNavigationBarItem(
                icon: Icon(AnanasIcons.help), title: Text("Help")),
          ],
        ),
      ),
    );
  }
}

This is the Home Screen from where I'm accessing the Drawer:
class Home extends StatelessWidget {
  final AppKeys appKeys = AppKeys();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
            icon: Icon(
              AnanasIcons.menu,
              color: Colors.black,
            ),
            onPressed: () {
              appKeys.homeKey.currentState.openDrawer();
            }),
        backgroundColor: Theme.of(context).canvasColor,
        title: Text("Hi"),
        actions: [
          IconButton(
              icon: Icon(
                Icons.person,
                color: Colors.black,
              ),
              onPressed: () {
                Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => Profile(),
                    ));
              })
        ],
      ),
      body: Container(),
    );
  }
}


A: I've found the answer! Instead of pushing the route with the key, we need to remove all the routes from the stack till the screen where we have to go.
Here's the code for that:
Navigator.of(context).popUntil(ModalRoute.withName(Base.routeName));

