You need to enable JavaScript to run this app. Flutter开发中home.dart代码不生效、UI不更新及路由报错求助
优惠活动
大模型
产品
解决方案
定价
更多

Flutter开发中home.dart代码不生效、UI不更新及路由报错求助

项目异常说明

我是一名正在开发校园项目的学生,对编程尚不熟悉。我的Flutter项目此前运行一切正常,近期出现两个核心问题:

  • home.dart 代码更新不生效:无论修改代码逻辑、调整相关变量取值,运行时始终加载旧版本home.dart的内容,其余.dart文件的修改均可正常生效。我修改了AppBar部分的配置,界面没有同步更新,同时还会触发仅旧代码逻辑才会出现的报错。
  • 运行中会触发路由找不到的异常,导致设备连接断开。
涉及的home.dart代码片段
class _HomeState extends State<Home> {
  List<PopularTourModel> popularTourModels = new List();
  List<CountryModel> country = new List();


  
  @override
  void initState() {
    country = getCountrys();
    popularTourModels = getPopularTours();
    super.initState();
  }

  @override
  void didUpdateWidget(oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget != widget) {
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Samsun Tur'),
        actions: [
        ],
        elevation: 0.0,
      ),
      body: SingleChildScrollView(
        child: Container(
          padding: EdgeInsets.symmetric(horizontal: 24, vertical: 24),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                "BETA",
                style: TextStyle(
                    fontSize: 28,
                    color: Colors.black54,
                    fontWeight: FontWeight.w600),
              ),
              SizedBox(
                height: 8,
              ),
              Text(
                "Tarihi Mekanlar:",
                style: TextStyle(
                    fontSize: 20,
                    color: Colors.black54,
                    fontWeight: FontWeight.w600),
              ),
              SizedBox(
                height: 16,
              ),
              GestureDetector(
                onTap: () {
                  print('ÇALIŞTI, mı acaba?');
                },
                child: Container(
                  height: 240,
                  child: ListView.builder(
                      itemCount: country.length,
                      shrinkWrap: true,
                      physics: ClampingScrollPhysics(),
                      scrollDirection: Axis.horizontal,
                      itemBuilder: (context, index)
                      {
                        return CountryListTile(
                          label: country[index].label,
                          countryName: country[index].countryName,
                          noOfTours: country[index].noOfTours,
                          rating: country[index].rating,
                          imgUrl: country[index].imgUrl,
                        );
                      }),
                ),
              ),
              SizedBox(
                height: 8,
              ),
              Text(
                "Size en yakın yerler!",
                style: TextStyle(
                    fontSize: 20,
                    color: Colors.black54,
                    fontWeight: FontWeight.w600),
              ),
              SizedBox(
                height: 16,
              ),
              ListView.builder(
                  shrinkWrap: true,
                  physics: ClampingScrollPhysics(),
                  itemCount: popularTourModels.length,
                  itemBuilder: (context, index) {
                    return PopularTours(
                      desc: popularTourModels[index].desc,
                      imgUrl: popularTourModels[index].imgUrl,
                      title: popularTourModels[index].title,
                      price: popularTourModels[index].price,
                      rating: popularTourModels[index].rating,
                      mekan: popularTourModels[index].mekan,
                      desclong: popularTourModels[index].desclong,
                      latitude: popularTourModels[index].latitude,
                      longitude: popularTourModels[index].longitude,
                      distanceToUser: popularTourModels[index].distanceToUser,
                    );
                  })
            ],
          ),
        ),
      ),
    );
  }
}
运行时报错日志
════════ 手势操作捕获到异常 ═══════════════════════════════════════════════════════════════
处理手势操作时抛出以下断言异常:
在 _WidgetsAppState 中找不到路由 RouteSettings("lib/views/home", null) 对应的路由生成器。
请确认根应用组件已提供该路由的生成逻辑,路由检索按以下顺序执行:
1. 针对 "/" 路由,优先使用非空的 home 属性配置
2. 若未匹配,查询 routes 路由表中是否存在对应条目
3. 仍未匹配时,调用 onGenerateRoute 方法,该方法需要为所有未被 home、routes 匹配的合法路由返回非空组件
4. 所有规则均未命中时,最终调用 onUnknownRoute 方法
当前项目未配置 onUnknownRoute 方法。
异常抛出时的调用栈:
#0      _WidgetsAppState._onUnknownRoute.<anonymous closure> (package:flutter/src/widgets/app.dart:1175:9)
#1      _WidgetsAppState._onUnknownRoute (package:flutter/src/widgets/app.dart:1190:6)
#2      NavigatorState._routeNamed (package:flutter/src/widgets/navigator.dart:3388:36)
#3      NavigatorState.pushNamed (package:flutter/src/widgets/navigator.dart:3425:20)
#4      NavigatorState.popAndPushNamed (package:flutter/src/widgets/navigator.dart:3479:12)
...
处理器类型:"onTap"
手势识别器:TapGestureRecognizer#39fcc
  调试归属对象:GestureDetector
  识别状态:possible
  赢得手势竞争
  最终点击位置:Offset(26.9, 51.3)
  组件内相对点击位置:Offset(26.9, 27.3)
  点击按键:1
  已发送点击按下事件
════════════════════════════════════════════════════════════════════════════════════════════════════
与设备的连接已断开。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.15 06:06:09