Hot reload and hot restart help you see changes in your app instantly without losing your work. They make app development faster and easier.
Hot reload and hot restart in Flutter
Hot reload: Press 'r' in terminal or click the lightning bolt in IDE. Hot restart: Press 'R' in terminal or click the restart button in IDE.
// After changing a button color // Press 'r' in terminal or click lightning bolt // The app updates color without restarting
// After adding a new variable or changing main() // Press 'R' in terminal or click restart button // The app restarts fresh with new code
This app shows a counter and a button. When you press the button, the number increases.
If you change the text style or button color and use hot reload, the app updates instantly without losing the counter value.
If you use hot restart, the app restarts and the counter resets to zero.
import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatefulWidget { const MyApp({super.key}); @override State<MyApp> createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { int counter = 0; @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text('Hot Reload Demo')), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text('Counter: $counter', style: const TextStyle(fontSize: 24)), const SizedBox(height: 20), ElevatedButton( onPressed: () => setState(() => counter++), child: const Text('Increment'), ), ], ), ), ), ); } }
Hot reload works best for UI and logic changes inside widgets.
Hot restart is needed if you change global variables or main() function.
Using hot reload saves time because you don't lose app state like form inputs or navigation.
Hot reload updates code quickly and keeps app state.
Hot restart restarts app and resets state.
Use hot reload for small changes and hot restart for big changes.
Practice
hot reload and hot restart in Flutter?Solution
Step 1: Understand hot reload behavior
Hot reload updates the code quickly and keeps the current app state intact.Step 2: Understand hot restart behavior
Hot restart restarts the whole app and resets the app state to initial.Final Answer:
Hot reload updates code and keeps app state; hot restart resets app state. -> Option DQuick Check:
Hot reload = keeps state, Hot restart = resets state [OK]
- Confusing which method resets app state
- Thinking hot reload restarts the app
- Assuming both behave the same
Solution
Step 1: Identify hot reload command
Pressing lowercase 'r' in the terminal triggers hot reload during flutter run.Step 2: Differentiate from hot restart command
Pressing uppercase 'R' triggers hot restart, not hot reload.Final Answer:
Press 'r' in the terminal running flutter -> Option CQuick Check:
Lowercase 'r' = hot reload [OK]
- Pressing uppercase 'R' for hot reload
- Using wrong flutter CLI commands
- Assuming hot reload needs special flags
int counter = 0;
void increment() {
counter++;
}
// After hot reload, what is the value of counter if increment() was called 3 times before reload?Solution
Step 1: Understand hot reload effect on state
Hot reload updates code but keeps current app state, so counter value stays as before.Step 2: Apply increment calls before reload
Counter was incremented 3 times, so its value is 3 after hot reload.Final Answer:
3 -> Option BQuick Check:
Hot reload keeps state, so counter = 3 [OK]
- Assuming counter resets to 0
- Confusing hot reload with hot restart
- Thinking hot reload causes errors
Solution
Step 1: Know hot reload limitations
Hot reload does not re-run main() or reset app state, so changes in main() won't appear.Step 2: Use hot restart for main() changes
Hot restart restarts the app and runs main() again, applying changes.Final Answer:
Hot reload does not update main() function; hot restart is needed. -> Option AQuick Check:
Main() changes require hot restart [OK]
- Expecting hot reload to update main()
- Thinking hot reload works differently on platforms
- Restarting app manually instead of hot restart
Solution
Step 1: Consider app state complexity
Since state is complex and slow to initialize, preserving it saves time.Step 2: Choose method that keeps state
Hot reload updates UI code quickly without resetting state, ideal for this case.Step 3: Avoid hot restart or manual restart
These reset state and cause delay due to reinitialization.Final Answer:
Use hot reload to update UI and keep state. -> Option AQuick Check:
Keep state with hot reload for fast UI testing [OK]
- Using hot restart and losing state
- Restarting app manually unnecessarily
- Trying to hot reload after main() changes
Ready to prove it?
Pass the test and this pattern moves unknown
