Skip to content

Beginner · Flutter setup and your first app · topic 14 of 50 · 30 min

Project structure and hot reload

Explanation

flutter create makes a project with lib/main.dart (your Dart code), pubspec.yaml (packages and assets), platform folders (android, ios, web and others), test/ and analysis_options.yaml. flutter run starts the app. Hot reload (r) injects code changes in about a second and keeps the app's state; hot restart (R) restarts the Dart code and resets state; some changes need a full restart.

Creating a project

bash
flutter create chai_shop
cd chai_shop
flutter run
Project names use lowercase with underscores. flutter run builds and starts the app on a connected device.

What is inside

output
chai_shop/
├── lib/
│   └── main.dart            <- your app starts here (main function)
├── test/
│   └── widget_test.dart     <- tests (module 10)
├── android/  ios/  web/  windows/  macos/  linux/   <- platform projects
├── assets/                  <- images, fonts (you create it and list it in pubspec)
├── pubspec.yaml             <- name, version, packages, assets
├── pubspec.lock             <- exact package versions
└── analysis_options.yaml    <- lint rules for code quality
Almost all your work happens in lib/. Platform folders are touched for permissions, icons and release settings.

The platform folders are real native projects. android/ is a normal Android Gradle project; ios/ is an Xcode project. Flutter generates them and you rarely edit them, except for things like the app name, permissions in AndroidManifest.xml or Info.plist, and signing for release (module 11).

The starter main.dart in brief

dart
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());     // put the root widget on the screen
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Chai Shop',
      theme: ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: Colors.brown)),
      home: const Scaffold(
        body: Center(child: Text('Namaste from Chai Shop')),
      ),
    );
  }
}
A simplified version of the template. You will learn every piece in the next topics.

Hot reload, hot restart, full restart

ActionHowWhat happensState kept?
Hot reloadPress r in the terminal, or save in VS Code/Android StudioNew code is injected and build methods re-runYes
Hot restartPress RDart code restarts from main()No, state resets
Full restartStop and flutter run againNative app rebuilt and reinstalledNo

Hot reload is Flutter's superpower: you change a colour, a padding or some text and see it on the phone in about a second, still on the same screen with the same data. Some changes are not picked up by hot reload, such as changes to main(), to initState of a widget that already exists, to global variables' initial values, or to native code and pubspec packages. If a change "does not show", try hot restart, and for native or package changes, a full restart.

Think of it like this: Hot reload is like a tailor adjusting the sleeve while you are still wearing the shirt. Hot restart is taking the shirt off and putting it on again. A full restart is stitching a new shirt from the cloth.

DevTools

Flutter DevTools is a set of debugging tools that opens in the browser or inside your editor: the widget inspector (see the widget tree and layout), a performance view, a memory view, network and logging views. You will use it in module 9; open it once now from your editor to see your app's widget tree.

Common mistakes

  • Expecting hot reload to apply changes to main() or initState: use hot restart.
  • Adding a package and only hot reloading: stop and run again after pub get adds native code.
  • Editing generated files in build/: they are overwritten.
  • Deleting platform folders you do not think you need; recreate them with flutter create . if needed.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

Project structure and hot reload · 1/6

flutter create | flutter run

flutter create makes a project. flutter run starts it on a device.

Prefer a video by a teacher? Find free videos on “flutter project structure hot reload vs hot restart explained” on YouTube ↗

In real life: Designing a menu screen with a shop owner

A freelance developer sits with a restaurant owner to design the menu screen. With the app running on the owner's phone, every request ("bigger prices", "green instead of red", "move the photo left") is a quick edit and save; hot reload shows the result in a second, still on the same screen. The design is finished in one meeting instead of a week of screenshots over WhatsApp.

Practice (0/3 done)

Task 1 · Question

You changed the initial value of a counter in initState and hot reloaded, but nothing changed. What should you do?

Task 2 · Question

Where does the code you write mostly live in a Flutter project?

Task 3 · Practice

Create a project with flutter create, run it on a device, change the text and theme colour and hot reload. Then add a counter value, change its starting number and notice that hot reload keeps the old value while hot restart resets it. Open DevTools and find your Text widget in the widget inspector.

Quick quiz

Question 1

1.What does hot reload keep?

Question 2

2.Which key triggers hot restart in the flutter run terminal?

Question 3

3.What is analysis_options.yaml for?

Question 4

4.What does runApp do?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

What does hot reload keep?

Answer: The app's current state

That is what makes it fast for UI work.

Which key triggers hot restart in the flutter run terminal?

Answer: R

Lowercase r is hot reload; capital R is hot restart.

What is analysis_options.yaml for?

Answer: Lint rules and static analysis settings

It configures the analyser.

What does runApp do?

Answer: Puts the root widget on the screen

It starts the widget tree.

What makes hot reload possible during development?Related

Answer: Dart's JIT compilation in debug mode

JIT lets code be swapped into the running app.

From Flutter and Dart: the big picture

Which needs a hot restart rather than a hot reload?Related

Answer: Changing the initial value set in initState of an existing widget

initState does not run again on reload.

From Interview questions: Flutter

Which command downloads the packages listed in pubspec.yaml?Related

Answer: flutter pub get

pub get resolves and downloads dependencies.

From Packages and pub

Does the Flutter SDK include Dart?Related

Answer: Yes, Dart is bundled

The Flutter SDK ships with the Dart SDK.

From Installing Flutter

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 2 more topics in “Flutter setup and your first app” before its exam: Everything is a widget, Stateless and stateful widgets.
  2. Module 4 exam: Flutter setup and your first app · 12 questions · 20 min, without help. 4 topics of this module still to finish first.
  3. Then the next module, Layouts. 50 topics left in the course before the final exam.

← Previous: Installing Flutter