Sub topic 1
Why Hot Reload Exists: Debug Mode and the Dart VM
Flutter hot reload exists because Dart runs inside a development environment that can accept changed Dart code while the app is still running. This environment is the Dart virtual machine, or Dart VM. In debug mode, the VM uses just-in-time compilation, often shortened to JIT. JIT means that Dart code is compiled while the program runs instead of being compiled completely before the app starts.
This combination lets Flutter update parts of a running application without rebuilding and relaunching the whole app.
Prerequisites
This chapter assumes that you know basic Dart syntax, can build simple Flutter widgets, understand the difference between StatelessWidget and StatefulWidget, and can start an app with flutter run or an IDE.
Debug Mode and JIT Compilation
When you run a Flutter app for development, you usually start it in debug mode:
flutter runFlutter builds the app and connects it to the Dart VM. The VM keeps information about the running program, including objects in memory and the code used by those objects.
When you save a Dart file, Flutter compares the new source with the code already loaded in the VM. If the change can be applied safely, Flutter sends the updated code to the VM. This is the first part of hot reload.
After the VM accepts the new code, Flutter asks the framework to rebuild the relevant widget tree. The build methods run again, so changed labels, layouts, colors, and other widget descriptions can appear immediately.
Why Debug Mode Is Required
Debug mode prioritizes development speed and inspection. It includes the Dart VM connection, debugging tools, assertions, and support for hot reload.
Profile and release builds use different compilation strategies. They optimize performance and package the application for measurement or distribution. They do not provide the same live code-update workflow. Therefore, hot reload and hot restart are development features, not production features.
| Mode | Main purpose | Hot reload |
|---|---|---|
| Debug | Development and debugging | Available |
| Profile | Performance measurement | Not available |
| Release | Distribution to users | Not available |
The Difference Between Code and Runtime State
The VM holds both program code and runtime state. Runtime state is the data currently held by objects, such as the selected tab, a text field's contents, or a counter value.
Hot reload updates eligible code while preserving those existing objects. Flutter then rebuilds widgets using the preserved objects. This is why you can edit a widget's build method and see the result without losing your place in the app.
The basic model is:
- The VM receives changed Dart code.
- Existing objects remain in memory.
- Flutter calls rebuild-related logic.
- The screen reflects the new widget code and old runtime data.
Hot reload cannot preserve every possible change. Some edits affect how the program starts or how objects are initialized. Those cases require hot restart or a full restart, which later chapters compare in detail.
A Useful Mental Model
Think of debug mode as a workshop. The Dart VM is the running workbench, and hot reload replaces an eligible tool while leaving the materials on the bench. The application keeps its current objects, but Flutter gets a chance to describe the interface again with the new code.
This model explains both the speed and the limits of hot reload. It is fast because Flutter does not recreate everything. It is limited because some changes need the program to start again.
Exercise
Why can hot reload update a widget's build method without resetting a counter stored in an existing State object?
Show answer
Debug mode sends eligible updated code to the Dart VM, while the existing State object remains in memory. Flutter rebuilds the widget tree using the new build method and the preserved counter value.