Flutter 热加载和热重启的区别reload restart

As a general rule, if the modified code is downstream of the root widget’s build method, then hot reload behaves as expected. However, if the modified code won’t be re-executed as a result of rebuilding the widget tree, then you won’t see its effects after hot reload

What is the difference between hot reload, hot restart, and full restart?

  • Hot reload loads code changes into the VM and re-builds the widget tree, preserving the app state; it doesn’t rerun main() or initState(). (⌘\ in Intellij and Android Studio, ⌃F5 in VSCode)
  • Hot restart loads code changes into the VM, and restarts the Flutter app, losing the app state. (⇧⌘\ in IntelliJ and Android Studio, ⇧⌘F5 in VSCode)
  • Full restart restarts the iOS, Android, or web app. This takes longer because it also recompiles the Java / Kotlin / ObjC / Swift code. On the web, it also restarts the Dart Development Compiler. There is no specific keyboard shortcut for this; you need to stop and start the run configuration.

Flutter web currently supports hot restart but not hot reload.

### Flutter 热重载与热更新的使用方法及原理 #### 使用方法 Flutter 的热重载功能可以通过 IDE 或命令行工具轻松触发。在支持 Flutter 开发的环境中(如 Android Studio、VS Code),只需点击“Hot Reload”按钮即可完成操作[^2]。如果偏好命令行方式,则可以在终端输入 `flutter run` 启动应用后,按键盘上的 `r` 键来触发热重载。 对于更复杂的场景,比如需要完全重建状态的情况,可以按下 `R` 键来进行 Hot Restart[^3]。需要注意的是,Hot Restart 会清除当前的应用状态并重新初始化整个应用程序。 #### 实现原理 Flutter 的热重载依赖于 Dart 虚拟机 (VM) 提供的支持以及 JIT AOT 编译模式的区别。具体来说: 1. **JIT 模式下的动态注入** 在开发阶段,默认采用 Just-In-Time (JIT) 编译器运行代码。当开发者发起热重载请求时,系统会对新旧版本的代码进行对比分析,提取出发生变化的部分,并将其作为增量数据发送给 Dart VM 进行实时加载替换[^4]。 2. **类对象的状态管理** 当新的字节码被成功注入到 Dart VM 中之后,框架会尝试保留现有实例及其内部状态不变的前提下,仅更新那些发生改变的方法定义或静态变量值[^1]。这种机制使得大多数情况下能够保持原有 UI 组件的状态不受影响。 3. **Widget 树的重新构建** 完成上述过程以后,Flutter 框架本身还会调用 setState 方法通知 widget tree 发生变化,从而触发界面渲染管道再次工作以展示最新的视觉效果。 以下是模拟实现的一个简化版伪代码示例: ```dart void hotReload() { // 对比前后两份源码差异 List<String> changedFiles = compareCodebase(); // 将变更部分传输至 Dart VM 并执行更新逻辑 dartVm.updateClasses(changedFiles); // 刷新 Widget Tree 显示最新改动结果 flutterFramework.rebuildWidgets(); } ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

computerclass

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值