Answer
Overview
Hot Reload and Hot Restart are Flutter's development features that speed up development by quickly applying code changes without restarting the entire app.
Hot Reload
Definition: Injects updated source code files into the running Dart VM (Virtual Machine) and rebuilds the widget tree while preserving app state.
How It Works
- You make a code change
- Press in terminal or click ⚡ in IDEtext
r - Flutter injects changed code into running VM
- Widget tree rebuilds
- App state is preserved (variables, navigation stack, etc.)
Example
dartclass CounterPage extends StatefulWidget { _CounterPageState createState() => _CounterPageState(); } class _CounterPageState extends State<CounterPage> { int _counter = 0; void _increment() => setState(() => _counter++); Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('Counter')), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text('Count: $_counter', style: TextStyle(fontSize: 48)), // Change this text color from blue to red Text('Tap + to increment', style: TextStyle(color: Colors.blue)), ], ), ), floatingActionButton: FloatingActionButton( onPressed: _increment, child: Icon(Icons.add), ), ); } }
Scenario:
- Run app, tap + button 5 times → text
_counter = 5 - Change totext
Colors.bluein codetextColors.red - Press (hot reload)text
r - Result: Text color changes to red, BUT still shows 5 (state preserved)text
_counter
When Hot Reload Works
✅ Works for:
- Changing UI (colors, text, layouts)
- Modifying methodstext
build() - Adding/removing widgets
- Updating business logic
- Changing styles and themes
dart// Before Text('Hello', style: TextStyle(color: Colors.blue)) // After (Hot Reload ✅) Text('Hello World!', style: TextStyle(color: Colors.red, fontSize: 20))
When Hot Reload Doesn't Work
❌ Doesn't work for:
- Changing app entry point ()text
main() - Changing global variables/constants
- Modifying class fields (non-final)
- Adding/removing class fields
- Changing enums
- Changing generic type parameters
- Native code changes (Android/iOS)
- Asset changes (images, fonts)
dart// Before class User { String name; User(this.name); } // After (Hot Reload ❌ - need Hot Restart) class User { String name; int age; // New field added User(this.name, this.age); }
Hot Restart
Definition: Restarts the entire app from scratch without restarting the underlying process. All app state is lost.
How It Works
- Destroys current widget tree
- Resets all variables to initial values
- Re-runs functiontext
main() - Rebuilds app from scratch
- Does NOT restart native platform code
Example
dartvoid main() { print('App started at ${DateTime.now()}'); // This prints again on Hot Restart runApp(MyApp()); } class MyApp extends StatelessWidget { Widget build(BuildContext context) { return MaterialApp(home: CounterPage()); } }
Scenario:
- Run app, tap + button 5 times → text
_counter = 5 - Press (hot restart)text
R - Result: App restarts, resets to 0,text
_counterruns againtextmain()
When to Use Hot Restart
Use when:
- Hot Reload doesn't apply changes
- You changed or global statetext
main() - You modified class structure
- App is in a broken state
- You want to test from initial state
Full Restart (Cold Restart)
Definition: Completely kills and restarts the app AND the underlying platform process.
When Required
- Native code changes (Android/iOS)
- Gradle/CocoaPods dependency changes
- Asset changes (pubspec.yaml)
- Changing app permissions
How to Full Restart
bash# Stop app flutter run --release # or Ctrl+C # Then restart flutter run
Comparison Table
| Feature | Hot Reload | Hot Restart | Full Restart |
|---|---|---|---|
| Trigger | text | text | Stop + Run |
| Speed | < 1 second | 1-3 seconds | 5-30 seconds |
| State preserved | ✅ Yes | ❌ No | ❌ No |
| Re-runs main() | ❌ No | ✅ Yes | ✅ Yes |
| Widget tree | Rebuilds | Rebuilds from scratch | Rebuilds from scratch |
| Native restart | ❌ No | ❌ No | ✅ Yes |
| Use for | UI changes | Code structure changes | Native/asset changes |
Detailed Behavior Examples
Hot Reload - State Preservation
dartclass LoginPage extends StatefulWidget { _LoginPageState createState() => _LoginPageState(); } class _LoginPageState extends State<LoginPage> { final _emailController = TextEditingController(); final _passwordController = TextEditingController(); Widget build(BuildContext context) { return Column( children: [ TextField(controller: _emailController), TextField(controller: _passwordController), // Change button color ElevatedButton( style: ElevatedButton.styleFrom(backgroundColor: Colors.blue), // Change to red onPressed: () {}, child: Text('Login'), ), ], ); } }
Steps:
- Type "test@example.com" in email field
- Type "password123" in password field
- Change totext
Colors.bluetextColors.red - Hot Reload ()text
r - Result: Button color changes BUT text fields still contain the typed values
Hot Restart - State Loss
Same code, but after Hot Restart:
- Type "test@example.com" in email field
- Type "password123" in password field
- Hot Restart ()text
R - Result: Text fields are cleared (state lost)
Keyboard Shortcuts
VS Code
| Action | Shortcut |
|---|---|
| Hot Reload | text text |
| Hot Restart | text text |
Android Studio / IntelliJ
| Action | Shortcut |
|---|---|
| Hot Reload | text text |
| Hot Restart | text text |
Terminal
bashr # Hot Reload R # Hot Restart q # Quit h # Help
Common Issues & Solutions
Issue: "Hot Reload Not Working"
Causes:
- Code change requires Hot Restart
- Syntax error in code
- App is in broken state
Solution:
- Check for syntax errors
- Try Hot Restart ()text
R - If still failing, do Full Restart
Issue: "Changes Not Visible After Hot Reload"
Cause: Hot Reload doesn't work for certain changes (enums, global variables, etc.)
Solution: Use Hot Restart instead
Issue: "App Crashes After Hot Reload"
Cause: State inconsistency
Solution: Hot Restart to reset state
Best Practices
| Practice | Recommendation |
|---|---|
| Default action | Use Hot Reload ( text |
| State testing | Use Hot Restart to test from initial state |
| After errors | Hot Restart if Hot Reload causes issues |
| Code structure changes | Always Hot Restart |
| Before testing | Hot Restart to ensure clean state |
Performance Comparison
textHot Reload: ~300ms - 1 second ⚡ Fastest Hot Restart: ~1-3 seconds 🔄 Fast Full Restart: ~5-30 seconds 🐢 Slow
Key Takeaways
Hot Reload: Fast UI changes, state preserved, use for most development.
Hot Restart: Resets app, state lost, use when Hot Reload doesn't work.
Full Restart: Complete rebuild, use for native/asset changes.