Bird
Raised Fist0
Fluttermobile~20 mins

Hot reload and hot restart in Flutter - Mini App: Build & Ship

Choose your learning style10 modes available
Build: Hot Reload Demo
A simple Flutter screen to demonstrate hot reload and hot restart by changing text and color dynamically.
Target UI
-------------------------
| Hot Reload Demo       |
|-----------------------|
| Text: Hello World     |
| Color: Blue           |
|                       |
| [Change Text] [Change Color] |
-------------------------
Display a text widget with initial text 'Hello World'.
Display a colored container below the text with initial color blue.
Add two buttons: 'Change Text' and 'Change Color'.
When 'Change Text' is tapped, update the text to 'Hello Flutter!'.
When 'Change Color' is tapped, update the container color to green.
Use hot reload to see text changes without losing state.
Use hot restart to reset the app to initial state.
Starter Code
Flutter
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: HotReloadDemo(),
    );
  }
}

class HotReloadDemo extends StatefulWidget {
  const HotReloadDemo({super.key});

  @override
  State<HotReloadDemo> createState() => _HotReloadDemoState();
}

class _HotReloadDemoState extends State<HotReloadDemo> {
  String displayText = 'Hello World';
  Color boxColor = Colors.blue;

  // TODO: Add methods to change text and color

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Hot Reload Demo')),
      body: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(displayText, style: const TextStyle(fontSize: 24)),
            const SizedBox(height: 20),
            Container(
              width: 100,
              height: 100,
              color: boxColor,
            ),
            const SizedBox(height: 40),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                ElevatedButton(
                  onPressed: () {
                    // TODO: Change text
                  },
                  child: const Text('Change Text'),
                ),
                ElevatedButton(
                  onPressed: () {
                    // TODO: Change color
                  },
                  child: const Text('Change Color'),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
Task 1
Task 2
Solution
Flutter
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: HotReloadDemo(),
    );
  }
}

class HotReloadDemo extends StatefulWidget {
  const HotReloadDemo({super.key});

  @override
  State<HotReloadDemo> createState() => _HotReloadDemoState();
}

class _HotReloadDemoState extends State<HotReloadDemo> {
  String displayText = 'Hello World';
  Color boxColor = Colors.blue;

  void changeText() {
    setState(() {
      displayText = 'Hello Flutter!';
    });
  }

  void changeColor() {
    setState(() {
      boxColor = Colors.green;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Hot Reload Demo')),
      body: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(displayText, style: const TextStyle(fontSize: 24)),
            const SizedBox(height: 20),
            Container(
              width: 100,
              height: 100,
              color: boxColor,
            ),
            const SizedBox(height: 40),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                ElevatedButton(
                  onPressed: changeText,
                  child: const Text('Change Text'),
                ),
                ElevatedButton(
                  onPressed: changeColor,
                  child: const Text('Change Color'),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

This Flutter app shows a text and a colored box. Two buttons let you change the text and the color.

When you tap 'Change Text', the text changes from 'Hello World' to 'Hello Flutter!'. When you tap 'Change Color', the box color changes from blue to green.

Using hot reload after changing the code updates the UI instantly without losing the current state (like the changed text or color). Using hot restart resets the app to its initial state, so the text and color go back to 'Hello World' and blue.

This helps you see code changes quickly and understand how state works in Flutter.

Final Result
Completed Screen
-------------------------
| Hot Reload Demo       |
|-----------------------|
| Text: Hello Flutter!  |
| Color: Green          |
|                       |
| [Change Text] [Change Color] |
-------------------------
Tap 'Change Text' button: text changes to 'Hello Flutter!' immediately.
Tap 'Change Color' button: box color changes to green immediately.
Hot reload updates code without resetting text or color.
Hot restart resets text to 'Hello World' and color to blue.
Stretch Goal
Add a reset button that sets text and color back to initial values.
💡 Hint
Create a new method that sets displayText to 'Hello World' and boxColor to Colors.blue, then call setState.

Practice

(1/5)
1. What is the main difference between hot reload and hot restart in Flutter?
easy
A. Both hot reload and hot restart reset app state.
B. Hot reload resets app state; hot restart updates code without resetting state.
C. Both hot reload and hot restart keep app state unchanged.
D. Hot reload updates code and keeps app state; hot restart resets app state.

Solution

  1. Step 1: Understand hot reload behavior

    Hot reload updates the code quickly and keeps the current app state intact.
  2. Step 2: Understand hot restart behavior

    Hot restart restarts the whole app and resets the app state to initial.
  3. Final Answer:

    Hot reload updates code and keeps app state; hot restart resets app state. -> Option D
  4. Quick Check:

    Hot reload = keeps state, Hot restart = resets state [OK]
Hint: Hot reload keeps state; hot restart resets state [OK]
Common Mistakes:
  • Confusing which method resets app state
  • Thinking hot reload restarts the app
  • Assuming both behave the same
2. Which of the following is the correct Flutter command to perform a hot reload?
easy
A. flutter hot-reload
B. flutter run --restart
C. Press 'r' in the terminal running flutter
D. Press 'R' in the terminal running flutter

Solution

  1. Step 1: Identify hot reload command

    Pressing lowercase 'r' in the terminal triggers hot reload during flutter run.
  2. Step 2: Differentiate from hot restart command

    Pressing uppercase 'R' triggers hot restart, not hot reload.
  3. Final Answer:

    Press 'r' in the terminal running flutter -> Option C
  4. Quick Check:

    Lowercase 'r' = hot reload [OK]
Hint: Press lowercase 'r' for hot reload in terminal [OK]
Common Mistakes:
  • Pressing uppercase 'R' for hot reload
  • Using wrong flutter CLI commands
  • Assuming hot reload needs special flags
3. Consider this Flutter app code snippet:
int counter = 0;

void increment() {
  counter++;
}

// After hot reload, what is the value of counter if increment() was called 3 times before reload?
medium
A. 0
B. 3
C. 1
D. Error: counter reset

Solution

  1. Step 1: Understand hot reload effect on state

    Hot reload updates code but keeps current app state, so counter value stays as before.
  2. Step 2: Apply increment calls before reload

    Counter was incremented 3 times, so its value is 3 after hot reload.
  3. Final Answer:

    3 -> Option B
  4. Quick Check:

    Hot reload keeps state, so counter = 3 [OK]
Hint: Hot reload keeps variable values unchanged [OK]
Common Mistakes:
  • Assuming counter resets to 0
  • Confusing hot reload with hot restart
  • Thinking hot reload causes errors
4. You made a change to your Flutter app's main() function but after hot reload, the change does not appear. What is the likely cause?
medium
A. Hot reload does not update main() function; hot restart is needed.
B. Hot reload always updates main(), so this is a bug.
C. You need to stop and restart the app manually.
D. Hot reload only works on Android, not iOS.

Solution

  1. Step 1: Know hot reload limitations

    Hot reload does not re-run main() or reset app state, so changes in main() won't appear.
  2. Step 2: Use hot restart for main() changes

    Hot restart restarts the app and runs main() again, applying changes.
  3. Final Answer:

    Hot reload does not update main() function; hot restart is needed. -> Option A
  4. Quick Check:

    Main() changes require hot restart [OK]
Hint: Main() changes need hot restart, not hot reload [OK]
Common Mistakes:
  • Expecting hot reload to update main()
  • Thinking hot reload works differently on platforms
  • Restarting app manually instead of hot restart
5. You have a Flutter app with a complex state that takes time to initialize. You want to quickly test UI changes without losing the current state. Which approach is best?
hard
A. Use hot reload to update UI and keep state.
B. Use hot restart to reload app and reset state.
C. Stop and restart the app manually.
D. Modify main() and then hot reload.

Solution

  1. Step 1: Consider app state complexity

    Since state is complex and slow to initialize, preserving it saves time.
  2. Step 2: Choose method that keeps state

    Hot reload updates UI code quickly without resetting state, ideal for this case.
  3. Step 3: Avoid hot restart or manual restart

    These reset state and cause delay due to reinitialization.
  4. Final Answer:

    Use hot reload to update UI and keep state. -> Option A
  5. Quick Check:

    Keep state with hot reload for fast UI testing [OK]
Hint: Use hot reload to keep state and update UI fast [OK]
Common Mistakes:
  • Using hot restart and losing state
  • Restarting app manually unnecessarily
  • Trying to hot reload after main() changes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard