Sub topic 13
Platform and Build-Mode Caveats
This chapter assumes you can run an app with flutter run, and that you know the difference between hot reload, hot restart, and a full restart from earlier chapters. It covers where those tools stop working. The limits come from the build mode, the target platform, and the part of the app you edited (Dart code or native code).
Hot reload is a debug-mode feature
Hot reload and hot restart depend on the Dart VM running your code with just-in-time (JIT) compilation. In JIT mode, the tooling sends changed source to the running VM. The VM updates class and function definitions in place, and Flutter then rebuilds the widget tree.
Flutter has three build modes. Only debug mode uses JIT.
| Mode | Command | Compilation | Hot reload and restart | Typical use |
|---|---|---|---|---|
| Debug | `flutter run` | JIT, with assertions and debugging services | Yes | Daily development |
| Profile | `flutter run --profile` | AOT, with limited profiling hooks | No | Measuring performance |
| Release | `flutter run --release` | AOT, optimized | No | Testing what users get |
Ahead-of-time (AOT) compilation turns Dart into machine code before the app starts. The result has no VM that can accept new source, so there is nothing to reload into. In profile and release runs, the terminal does not offer r or R. To apply an edit, stop the app and run it again.
flutter run # debug: r and R work
flutter run --profile # AOT: no reload, profile with DevTools
flutter run --release # AOT: no reload, no debugging servicesReplace nothing above; the diagram follows.
Behavior that differs between debug and release
Because debug mode is a different build, the app behaves differently in several ways. Do not judge speed or app size from a debug run.
- Performance. Debug builds are slower. Janky animations in debug often disappear in profile mode.
- Assertions.
assertstatements run only in debug mode. Release builds skip them, so anassertcannot be your only input validation. - Debug helpers. Debug mode also adds extra checks, such as the layout overflow stripes and the debug banner.
- Size. A debug build includes the debugging services and unoptimized code, so it is much larger than a release build.
The constants kDebugMode, kProfileMode, and kReleaseMode from package:flutter/foundation.dart let you see which build you are running. This program prints them:
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
const apiHost = String.fromEnvironment('API_HOST', defaultValue: 'localhost');
void main() {
assert(() {
debugPrint('Assertions are enabled, so this is a debug build.');
return true;
}());
debugPrint(
'debug=$kDebugMode profile=$kProfileMode release=$kReleaseMode '
'apiHost=$apiHost',
);
runApp(const MaterialApp(home: Scaffold(body: Center(child: Text('Modes')))));
}Run it with flutter run, then with flutter run --release. In release you see the second line only. The assert block is skipped. Release builds also hide most debugPrint output on some platforms, so rely on DevTools or logging for release diagnostics.
Compile-time values need a full restart
String.fromEnvironment reads a value that --dart-define supplies when the app is built. The result is a compile-time constant. The main function above also ties into a rule from the previous chapter: edits to main are not applied by hot reload.
flutter run --dart-define=API_HOST=staging.example.comTo change API_HOST, stop the app and run it again with a new flag. Hot restart reuses the same launch configuration, so it does not apply a new --dart-define value either. The same applies to --flavor and --dart-define-from-file.
Platform differences
Flutter's tooling is the same on every platform, but the surrounding system is not. These are the points that trip people up.
Android
Hot reload works on emulators and physical devices. Dart edits go through the VM. Edits to Kotlin or Java code, AndroidManifest.xml, Gradle files, and native plugin code are part of the native Android app. Hot reload does not touch them. You must stop the app and run it again so Gradle can rebuild the Android project.
iOS
The same split applies. Dart code reloads. Swift, Objective-C, Info.plist, entitlements, and CocoaPods changes need a rebuild.
On physical devices running iOS 14 or later, a debug build can only be launched from Flutter tooling, an IDE, or Xcode. If you tap the app icon on the home screen, the debug app may fail to start or show an error. Use a profile or release build when you want to launch the app by tapping its icon.
Profile mode is also unavailable on the iOS Simulator and the Android emulator, because profiling there does not reflect real hardware. Use a physical device for it.
Desktop
macOS, Windows, and Linux debug builds support hot reload and hot restart the same way mobile does. The caveat is the same native one: changes to the runner project (the C++, Swift, or Objective-C host code), to permissions such as macOS entitlements, or to build files need a full restart.
Web
The web differs most. Dart on the web is compiled to JavaScript, not run in the Dart VM, so the JIT mechanism does not apply. Historically, flutter run -d chrome supported hot restart, which rebuilds and reloads the app, but not stateful hot reload. Pressing r did not keep your state.
Recent Flutter versions have added hot reload on the web, first behind an experimental flag. Support depends on your version, so check what the terminal offers after flutter run -d chrome starts. The help line lists the keys that are available. If the web target only offers restart, plan your workflow around state resets, for example by keeping a way to jump back to the screen you are editing.
Native code, plugins, and dependencies
The rule that explains most platform caveats is that hot reload updates Dart code and nothing else. These changes need a full stop and run:
- Editing native source: Kotlin, Java, Swift, Objective-C, C++.
- Editing platform configuration:
AndroidManifest.xml,build.gradle,Info.plist, entitlements. - Adding a package that includes native code, such as a plugin that uses camera, location, or storage APIs. Even a pure Dart package may require a restart, because the tooling regenerates plugin registration and the dependency set changes.
- Changing
pubspec.yamlin ways that affect the build, such as new dependencies or font declarations.
After adding a dependency, run flutter pub get if your editor does not do it for you, and then stop and run the app.
Reload can be rejected
Even in debug mode on a supported platform, the VM can reject a reload. This happens when an edit changes something the VM cannot patch into live objects, such as turning an enum into a class or changing a class's generic type parameters in an incompatible way. Compile errors also block a reload. In both cases the app keeps running the old code, and the terminal prints a message. The usual fix is a hot restart, and if that fails, a full stop and run.
Attaching to a running app
flutter attach connects the tooling to an app that is already running in debug mode, for example one started from Xcode or Android Studio's native side. Once attached, r and R work as usual, because the app is a debug build with a VM service available. An attached release or profile build still has no reload.
flutter attach
# Then press r for hot reload or R for hot restart, as with flutter run.A quick decision guide
- The app was launched as
--profileor--release: stop and run again. - You changed Dart code in debug mode: use hot reload, or hot restart if the change touches
initState,main, or static and global initializers. - You changed native code, platform config, or dependencies with native code: stop and run again.
- You changed
--dart-define,--flavor, or the target device: stop and run again. - You are on the web: check which keys your version offers, and expect restart behavior if reload is missing.
Exercise
You run flutter run --release on a physical phone, edit a Text widget in Dart, and press r. Nothing changes. Why, and what should you do?
Show answer
A release build is compiled AOT, so there is no Dart VM that can accept new code and hot reload is not available. Stop the app and run it again. If you want fast edit cycles, use a debug build with plain flutter run.
Exercise
You add a new --dart-define=API_HOST=staging.example.com value to your launch command while the debug app is running, then press R. The app still uses the old host. Explain why, and name the correct action.
Show answer
String.fromEnvironment produces a compile-time constant that is fixed when the app is built, and hot restart reuses the existing build and launch configuration. Stop the app and run flutter run again with the new --dart-define flag.
Exercise
Name two kinds of edits that hot reload never applies on Android or iOS even in debug mode, and say what you must do for them.
Show answer
Examples include edits to native code (Kotlin, Java, Swift, Objective-C) and edits to platform configuration such as AndroidManifest.xml, build.gradle, or Info.plist. Adding a plugin with native code also counts. In each case, stop the app and run it again so the native project is rebuilt.