Sub topic 13
Platform and Build-Mode Caveats
Assume you can build simple Flutter widgets, distinguish StatelessWidget from StatefulWidget, and run an app with flutter run or an IDE. This chapter focuses on why reload behavior changes across platforms and build modes.
Debug Mode Makes Reload Possible
Flutter hot reload depends on the Dart virtual machine (VM) running in debug mode. The VM uses just-in-time (JIT) compilation, which means it can compile updated Dart code while the app is running.
Start an application in debug mode with:
flutter run --debugWhen Flutter detects a Dart source change, hot reload sends the updated libraries to the running Dart VM. Flutter then rebuilds the widget tree. Existing State objects usually remain in memory.
Hot restart also uses the debug VM, but it restarts the Dart isolate. An isolate is Dart's independent execution environment. Restarting it runs main() again and creates new application state.
Debug, Profile, and Release
Flutter has three common build modes:
| Build mode | Main purpose | Hot reload |
|---|---|---|
| Debug | Development and debugging | Available |
| Profile | Measuring performance on a near-production app | Not available |
| Release | Shipping the application | Not available |
Profile mode removes some debugging support and measures performance more realistically. Release mode applies compiler optimizations and removes development features. Neither mode supports hot reload or hot restart.
For example, this command launches profile mode:
flutter run --profileIf you edit a Dart file while using profile mode, the running application will not receive the change. Stop the app and run it again, or switch back to debug mode.
Platform-Specific Limits
Hot reload mainly updates Dart code. It does not replace every part of the application.
Changes to native Android or iOS code usually need a full rebuild. Examples include:
- Editing a Kotlin or Java file in
android/ - Editing a Swift or Objective-C file in
ios/ - Adding or changing a native plugin
- Changing an Android manifest or iOS property list
- Changing native build settings
- Adding a platform permission
A full restart means stopping the application and starting it again. A rebuild may also be necessary when generated files, plugin registration, or native dependencies change.
Desktop targets have the same general rule. Dart widget edits can use debug reload, but changes to native desktop runner code or build configuration require rebuilding the application.
Flutter web has additional limitations. In web debug mode, hot reload can update Dart code, but browser-specific behavior, JavaScript interoperation, and some web configuration changes may require a browser refresh or a new build. A browser refresh also resets in-memory state.
Choosing the Right Action
Use this sequence when an edit does not appear:
- Use hot reload for a change inside
build, such as new text, layout, or colors. - Use hot restart when startup code,
main, or initial state must run again. - Stop and run the app again when native code, plugins, permissions, or build settings changed.
- Check that the app is running in debug mode.
- Check the terminal for compilation errors. Flutter cannot reload code that failed to compile.
A reload can succeed even when the visible result is unchanged. For example, changing a value assigned in initState does not change an existing State object. Hot restart reruns initState, so it applies the new initial value.
A Practical Decision Table
| Edit | First action | Reason |
|---|---|---|
| Change `Text` inside `build` | Hot reload | The widget can rebuild |
| Change a value in `initState` | Hot restart | Existing state already ran `initState` |
| Change `main()` | Hot restart | Startup code must run again |
| Change Kotlin, Swift, or permissions | Full restart and rebuild | Native code is outside the Dart reload |
| Run in profile mode | Stop and run again | Profile mode has no hot reload |
Exercise
You changed an Android manifest permission while the app was running in debug mode. Which action should you try, and why?
Show answer
Stop the app and run it again, allowing Flutter to rebuild the native Android application. Hot reload updates Dart code but does not apply manifest changes.