0

I am using the bottom navigation bar in my project.

After coming on the Home screen error is showing:

Failed assertion: line 3634 pos 18: '!keyReservation.contains(key)': is not true.

I tried 2-3 ways to solve this bug but can't able to solve it.

I put a key in every scaffold screen of pages but was not able to solve it.

class Home extends StatefulWidget {
@OverRide
_HomeState createState() => new _HomeState();
}
class _HomeState extends State {

late List pages = [ MyOrders(), new HomeScreen(), CartItems() ];

int _selectedIndex = 1;

_onItemTapped(dynamic index) {
    setState(() {
    _selectedIndex = index;
    });
   }

@OverRide
Widget build(BuildContext context) {
    return Scaffold(
     backgroundColor: Colors.white,
     bottomNavigationBar: BottomNavigationBar(
      items: const [
      BottomNavigationBarItem(
        icon: Icon(Icons.monetization_on_outlined),
        label: 'My Orders',
   ),
     BottomNavigationBarItem(
        icon: Icon(Icons.account_circle_outlined),
        label: 'Home',
   ),
    BottomNavigationBarItem(
        icon: Icon(Icons.work_outline),
        label: 'My Cart',
   ),
 ],
   currentIndex: _selectedIndex,  
   selectedItemColor: Color(0xff232869),
   onTap: _onItemTapped,
  ),
 body: pages[_selectedIndex],
      ) ;
   }
}
Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
Hemant
  • 1
  • 2
  • body: pages[_selectedIndex], //I this here is this error but the actual for removing it. I did not get it yet, – Hemant Oct 26 '21 at 11:29

1 Answers1

0

I faced same problem but in different case. I hope it will help you to understand the issue -

My case is - When user opens the app, on the first page (Home Screen), I check the network connection first before calling any API and if network is not available, User redirect to another page immediately (push from home to another screen) to show offline data. In this case I was getting same error -

Solutions works for me - before redirection I added delay for 1 second and it works fine.

await Future.delayed(const Duration(milliseconds: 1000));
Dharman
  • 30,962
  • 25
  • 85
  • 135