You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter Navigator.of路由报错:无法找到/new_note路由生成器

解决Flutter中pushNamed导航找不到路由的问题

你遇到的错误核心原因是没有在MaterialApp中配置路由映射表,和给FAB加Builder无关——Builder是用来获取正确的BuildContext,但这里系统根本不知道/new_note这个路径对应哪个页面组件。

具体修复步骤:

  1. 在main.dart中导入new_note.dart文件
  2. 给MaterialApp添加routes参数,配置路径与页面的对应关系

修改后的main.dart代码如下:

import 'package:flutter/material.dart';
import 'package:notes_app/pages/start_page.dart';
// 新增:导入NewNote页面
import 'package:notes_app/pages/new_note.dart';

void main() {
  runApp(const NoteMain());
}

class NoteMain extends StatelessWidget {
  const NoteMain({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: const StartPage(),
      // 新增:配置路由表
      routes: {
        '/new_note': (context) => const NewNote(),
      },
    );
  }
}

额外优化建议:

为了避免硬编码路由字符串导致拼写错误,可以在项目中定义一个路由常量文件,比如:

// 新建routes.dart文件
class AppRoutes {
  static const String newNote = '/new_note';
}

然后在导航和路由配置中使用AppRoutes.newNote代替字符串,这样更规范也更不容易出错。

内容的提问来源于stack exchange,提问作者appdev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 03:21:00