Sub topic 1
Why Hot Reload Exists: Debug Mode and the Dart VM
Hot reload can update a running app in about a second, with your screen position and data intact. It is not a feature of the Flutter framework alone. It depends on how the app is built and executed during development. This chapter explains that foundation: what debug mode is, why the Dart VM and just-in-time (JIT) compilation make code swapping possible, and why release builds cannot do it.
Prerequisites
This chapter assumes you can:
- Read and write basic Dart syntax, including classes,
finalandconst, andasyncbasics. - Build simple Flutter widgets and tell a
StatelessWidgetfrom aStatefulWidget. - Run an app with
flutter runor from an IDE.
You do not need to know how compilers work. The chapter introduces the terms it needs.
Three Build Modes
Flutter apps can be built in three modes. Each one answers a different question: how do I develop, how do I measure performance, and how do I ship.
| Mode | Command | Compilation | Hot reload | Typical use |
|---|---|---|---|---|
| Debug | `flutter run` (default) | JIT on the Dart VM | Yes | Daily development |
| Profile | `flutter run --profile` | AOT, with some tracing kept | No | Measuring performance |
| Release | `flutter run --release` or `flutter build` | AOT | No | Shipping to users |
AOT means ahead-of-time compilation. The Dart compiler turns your whole program into native machine code before the app is installed. JIT means just-in-time compilation. The program is loaded as an intermediate form and a virtual machine compiles it to machine code while the app runs.
Debug mode is the only mode that includes the machinery for changing code in a running app. The other two modes trade that ability for speed and smaller size.
What the Dart VM Provides
The Dart VM is a runtime that executes Dart programs. In debug mode your app runs inside it, and the VM brings several capabilities that a plain native binary lacks:
- A JIT compiler. The VM compiles code on demand, so it can accept new code at runtime.
- An interpretable intermediate form. The VM loads Dart code as a kernel file (
.dill). Kernel is Dart's compact binary representation of a program after parsing and type-checking. - Isolates. An isolate is Dart's unit of execution, with its own memory heap. Your UI code runs in the main isolate. Because the VM controls the isolate's heap and class table, it can swap out class definitions while keeping live objects.
- The VM Service. This is a protocol, usually spoken over a WebSocket, that tools use to talk to the running VM. DevTools, debuggers, and the
fluttercommand all use it. Reloading code is one of its requests.
A release build has none of this. It contains compiled machine code and a small runtime, with no JIT compiler, no kernel loader, and no service for receiving new code.
Other Things Debug Mode Changes
Debug mode is not only "release plus reload." It also changes how your code behaves:
- Assertions run.
assert(...)statements are checked in debug mode and removed in release mode. - Extra framework checks. The Flutter framework validates layout constraints, widget keys, and other invariants, and prints detailed errors when they break.
- Service extensions are registered. The framework exposes calls such as the widget inspector and the reassemble call that hot reload uses.
- Performance is lower. JIT warm-up, checks, and unoptimized code make debug apps slower and larger than release apps.
You can see the active mode with constants from package:flutter/foundation.dart.
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
void main() {
String mode;
if (kReleaseMode) {
mode = 'release';
} else if (kProfileMode) {
mode = 'profile';
} else {
mode = 'debug';
}
debugPrint('Running in $mode mode');
var assertsEnabled = false;
assert(() {
assertsEnabled = true;
return true;
}());
debugPrint('Assertions enabled: $assertsEnabled');
runApp(MaterialApp(
home: Scaffold(
body: Center(child: Text('Mode: $mode')),
),
));
}Run this once with flutter run and once with flutter run --release. The debug run prints debug and true. In the release run the text on screen says release. The log lines are not visible there, because debugPrint output is not shown in the terminal for release runs.
The assert(() { ...; return true; }()) idiom is a standard way to run code only in debug mode. The closure is evaluated only when assertions are on.
How a Reload Travels from Editor to Screen
When you save a file and trigger a hot reload, several programs cooperate. The flutter tool, the VM, and the framework each do one part.
The steps in more detail:
- Detect changes. The tool checks which source files changed since the last compile.
- Compile incrementally. A long-lived compiler process keeps the previous program in memory. It recompiles only the libraries affected by your edit and produces a small kernel delta. This is why a reload is fast even in a large project.
- Send to the VM. The tool asks the VM, through the VM Service, to load the delta into the running isolate.
- Update classes and functions. The VM replaces method bodies and class definitions with the new versions. Existing objects stay alive. The VM checks whether each existing object can still fit its updated class.
- Reassemble the framework. The tool then asks Flutter to reassemble the app. The framework marks every element in the widget tree as needing a rebuild, so
buildmethods run again with the new code. - Paint. The next frame shows your change.
Steps 4 and 5 explain two behaviors you will see throughout this tutorial. State survives because the VM keeps objects alive, including your State objects and their fields. New code appears because the framework calls build again, and build now runs the new method body.
Why Some Changes Cannot Be Applied
The same design explains the limits of hot reload. The VM can replace code, but it cannot rerun code that already finished. Consider where values come from:
- A
Stateobject's fields were set when the object was created. Code ininitStateran once at that time. Replacing theinitStatebody does not make it run again for an existing object. - A static field or a global variable is set the first time it is accessed, or at startup. Changing its initializer does not reset the value already stored.
mainran once when the app started. Nothing calls it again on reload.
Some structural changes can also be rejected by the VM. Examples are changing an enum into a regular class, or changing a class so that existing objects of that class no longer fit its new shape. In those cases the tool reports that the reload was rejected, and you need a hot restart. Later chapters in this tutorial cover each of these cases and how to choose the right action.
import 'package:flutter/material.dart';
// Initialized once, on first access. Editing 10 and hot reloading
// leaves the stored value unchanged.
int globalCounter = 10;
class Config {
static int retries = 3;
}
void main() {
// Runs once at startup. Edits here need a hot restart.
runApp(const MaterialApp(home: Scaffold(body: Center(child: Text('Hello')))));
}Hot restart works differently. It throws away the isolate's state, loads the new code, and runs main again. You lose state, but every initializer runs fresh. It still avoids reinstalling the app and relaunching the process, so it is faster than a full stop and run.
Why Release Builds Cannot Hot Reload
In a release or profile build, the Dart code was compiled to native machine code before installation. There is no kernel loader, no incremental compiler connection, and no VM Service endpoint ready to accept new code. Even if the tool could send a delta, nothing in the app could apply it.
This has practical consequences:
- Profile mode is for measuring performance, so you cannot hot reload while profiling. Measure first, then return to debug mode to change code.
- A bug that appears only in release mode must be investigated with
flutter run --release, and every change needs a full rebuild and reinstall. - Debug performance numbers do not reflect what users see. Never judge speed from a debug build.
Some platforms add their own rules. On a physical iOS device, for example, debug builds use JIT and can only be launched from developer tools, not from the home screen. Web builds use a different compiler pipeline in debug mode. A later chapter covers these differences.
Putting the Model Together
Keep this short model in mind for the rest of the tutorial:
- Debug mode runs your code in the Dart VM with JIT compilation.
- The VM can accept new code through the VM Service and swap it into live isolates without discarding objects.
- Flutter then rebuilds the widget tree so the new code is used.
- Anything that already ran once and stored its result is not rerun by hot reload.
- Release and profile builds are compiled ahead of time and cannot do any of this.
Exercise
You run flutter run --release, edit the text in a widget's build method, and press r. Nothing changes. Give two reasons from this chapter why hot reload cannot work in this run, and name the mode you should use to get hot reload back.
Show answer
Release builds are compiled ahead of time to native code, so the app has no JIT compiler or kernel loader to accept new code. They also have no service extensions or VM Service endpoint for the tool to send a kernel delta to, and no reassemble hook for the framework. Run in debug mode with plain flutter run, which uses the Dart VM with JIT.
Exercise
In debug mode, you change int globalCounter = 10; to int globalCounter = 20; and hot reload. The app still shows 10. Using the reload pipeline, explain why, and say what action fixes it.
Show answer
Hot reload replaces code in the running VM but does not rerun code that already ran. The initializer of globalCounter was evaluated once and its result is stored in the existing isolate, so the new initializer is not evaluated again. A hot restart resets the isolate and runs main and the initializers fresh, so the value becomes 20.