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

Flutter Android模拟器Hot reload失效,Chrome端正常问题求助

Flutter Android模拟器热重载失效问题分析与解决

核心原因

Flutter的热重载机制不会重新执行main()函数,也不会重新创建根Widget实例。你的代码直接在main()里通过runApp初始化了Text组件,修改Text内容后,热重载并不会触发main()的再次执行,所以模拟器里的UI不会更新。而Web端的热重载实现存在差异(或是你误触发了全量重启),才会看起来正常工作。

解决方法

将UI逻辑封装到StatelessWidget或StatefulWidget的build方法中,热重载会重新执行build方法,从而更新UI。

修改后的代码示例:

import 'package:flutter/material.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Center(
        child: Text(
          '?n ?nm \n hello the hot reload works',
          textDirection: TextDirection.ltr,
          textAlign: TextAlign.center,
          overflow: TextOverflow.visible,
          style: TextStyle(
            fontSize: 40.0,
            fontWeight: FontWeight.bold,
            foreground: Paint()
              ..color = Colors.yellow
              ..strokeWidth = 3.0
              ..style = PaintingStyle.fill,
            fontStyle: FontStyle.italic,
            shadows: [
              Shadow(color: Colors.black, offset: Offset(2.0, 1.0), blurRadius: 1.0)
            ]
          ),
        ),
      ),
    );
  }
}

额外说明

  • 热重载仅更新Widget的build方法、State的build方法以及已存在的State对象的字段修改,不会重新初始化全局变量、重新执行main或重新创建State对象(除非使用热重启)。
  • 如果需要彻底重启应用(比如修改了main函数或全局状态),可以使用热重启(点击IDE中的restart按钮,或执行flutter run --hot-restart)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 02:22:05