【问题标题】:Could not find a generator for route RouteSettings("/GeneralAnnouncements", null) in the _WidgetsAppState在 _WidgetsAppState 中找不到路由 RouteSettings(\"/GeneralAnnouncements\", null) 的生成器
【发布时间】:2022-12-04 09:07:31
【问题描述】:

当我运行它并按下按钮时出现错误(无法在 _WidgetsAppState 中找到路由 RouteSettings("/GeneralAnnouncements", null) 的生成器。所以我能知道这里的问题是什么吗?因为我只想使用命名路由所以请不建议我使用.push,因为它不适合我。问题出在Navigator.pushNamed(context, '/GeneralAnnouncements');这一行,但我不知道为什么?

这是我的代码:

class home extends StatelessWidget {
  const home({key}) : super(key: key);
  static const routeName = '/home';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Color(0xFF6D0131),
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            CircleAvatar(
                radius: (100),
                child: ClipRRect(
                  borderRadius: BorderRadius.circular(110),
                  child: Image.asset('images/logo.png'),
                )),
            SizedBox(
              height: 10.0,
            ),
            GestureDetector(
              onTap: () {
                Navigator.pushNamed(context, '/GeneralAnnouncements');
              },
              child: Padding(
                padding: const EdgeInsets.fromLTRB(8, 0, 8, 0),
                child: Container(
                  height: 100.0,
                  decoration: BoxDecoration(
                    color: Color(0xFF8D0235),
                    borderRadius: BorderRadius.circular(20.0),
                  ),
                  margin: EdgeInsets.only(bottom: 10.0),
                  padding: EdgeInsets.only(left: 18.0),
                  child: Center(
                    child: Text(
                      '        General \nAnnouncements',
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 35.0,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                ),
              ),
            ),
            
          ],
        ),
      ),
    );
  }
}

这是MaterialApp

runApp(MaterialApp(
    initialRoute: '/homepage',
    routes: {
      "/homepage": (context) => MyHomepage(),
      '/login': (context) => LoginScreen(),
      '/registration': (context) => RegistrationScreen(),
      "/GeneralAnnouncements": (context) => GeneralAnnouncements(),
      '/MyCalendar': (context) => MyCalendar(),
      "/home": (context) => home(),
    },
  ));

【问题讨论】:

    标签: flutter dart flutter-layout flutter-navigation


    【解决方案1】:

    从您的班级主页中删除 MaterialApp。你的代码是这样的:

    class home extends StatelessWidget {
      const home({key}) : super(key: key);
      static const routeName = '/home';
    
        @override
          Widget build(BuildContext context) {
            return MaterialApp( // Remove this and return Scaffold directly
              home: Scaffold(
                backgroundColor: Color(0xFF6D0131),
                body: Column(
    

    【讨论】:

      猜你喜欢
      • 2020-03-13
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 2022-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多