Flutter 使用底部导航栏时,找不到用于路由设置的生成器

Flutter 使用底部导航栏时,找不到用于路由设置的生成器,flutter,Flutter,我尝试使用以下方法实现底部导航栏: 但是,当我尝试导航到另一条路线,但应用程序没有重新路由时,我遇到了一个错误: 找不到每秒路由设置的生成器,在_WidgetsAppState中为null 我做错了什么?如何修复它?Ongeneraterroute需要MaterialPagerroute。当我使用底部导航栏时,我只有一个页面。然后,我将根据索引更改它显示的小部件。 void main() { runApp(MyApp()); } class MyApp extends StatelessW

我尝试使用以下方法实现底部导航栏:

但是,当我尝试导航到另一条路线,但应用程序没有重新路由时,我遇到了一个错误:

找不到每秒路由设置的生成器,在_WidgetsAppState中为null


我做错了什么?如何修复它?

Ongeneraterroute需要MaterialPagerroute。当我使用底部导航栏时,我只有一个页面。然后,我将根据索引更改它显示的小部件。
void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => SomeProvider(),
      child: MaterialApp(
        title: 'Cool app',
        home: Scaffold(
          body: Navigator(
            onGenerateRoute: (RouteSettings settings) {
              WidgetBuilder builder;

              switch (settings.name) {
                case '/first':
                  builder = (BuildContext context) => First();
                  break;

                case '/second':
                  builder = (BuildContext context) => Second();
                  break;

                case '/random':
                  builder = (BuildContext context) => Random();
                  break;

                case '/another-random':
                  builder = (BuildContext context) => AnotherRandom();
                  break;

                default:
                  throw Exception('Invalid route: ${settings.name}');
              }

              return MaterialPageRoute(builder: builder, settings: settings);
            },
          ),
          bottomNavigationBar: BottomNav(),
        ),
        initialRoute: '/first',
      ),
    );
  }
}

class BottomNav extends StatefulWidget {
  @override
  BottomNavState createState() => BottomNavState();
}

class BottomNavState extends State<BottomNav> {
  int _currentIndex = 0;
  @override
  Widget build(BuildContext context) {
    return CustomNavigationBar(
      iconSize: 30.0,
      selectedColor: Color(0xff040307),
      strokeColor: Color(0x30040307),
      unSelectedColor: Color(0xffacacac),
      backgroundColor: Colors.white,
      items: [
        CustomNavigationBarItem(unSelectedTitle: 'First', selectedTitle: 'First', icon: Icons.play_circle_filled),
        CustomNavigationBarItem(unSelectedTitle: 'Second', selectedTitle: 'Second', icon: Icons.play_circle_filled),
        CustomNavigationBarItem(unSelectedTitle: 'Third', selectedTitle: 'Third', icon: Icons.playlist_add_check)
      ],
      currentIndex: _currentIndex,
      onTap: (index) {
        switch (index) {

          case 0:
            Navigator.pushNamed(context, '/first');
            break;
          case 1:
            Navigator.pushNamed(context, '/second');
            break;
          case 2:
            Navigator.pushNamed(context, '/random');
            break;
          default:
        }
        setState(() {
          _currentIndex = index;
        });
      },
    );
  }
}