Sub topic 1
What Flutter and Dart are
Flutter is a toolkit for building user interfaces. Dart is the programming language you write it in. This chapter explains how the two fit together, so the commands in later chapters make sense: why flutter doctor checks what it checks, why hot reload works, and why a release build behaves differently from a debug build.
Prerequisites
This chapter assumes you already have the following:
- Working knowledge of variables, functions, classes, and control flow in any language.
- Comfort running commands in a terminal.
- A rough idea of what a mobile or desktop app is. You don't need to know Dart, Java, Kotlin, Swift, or C#. If you know one of them, Dart will look familiar.
You don't need to install anything to read this chapter. The next chapter covers installation.
What Flutter is
Flutter is an open-source UI framework. From one codebase it produces apps for Android, iOS, the web, Windows, macOS, and Linux.
Flutter differs from many cross-platform tools in how it draws the screen. It does not wrap the operating system's buttons, lists, and text fields. It draws every pixel itself, using its own rendering engine, onto a blank surface the operating system provides. A Flutter button looks the same on Android and iOS because the same code paints it on both. It does not look the same because two native buttons were styled to match.
This design has consequences:
- Your UI is consistent across platforms, and you control every detail of it.
- Flutter ships its own rendering code inside each app, so apps carry a baseline size cost.
- Native look and feel is something Flutter reproduces. The Material and Cupertino widget libraries imitate Android and iOS conventions, but they are Flutter's copies. When a platform redesigns its controls, those libraries must catch up.
The layers of Flutter
Flutter is built as layers. The diagram shows what sits between your code and the screen.
Each layer has a distinct job:
- Your app code is what you write: screens, logic, and data handling.
- The framework is a Dart library. It provides widgets (the building blocks of UI), layout, painting, animation, and gesture handling. Most of what you read and call lives here, and you can read its source in your editor.
- The engine is mostly C++. It runs the Dart code, lays out text, and rasterizes graphics. Its renderer is Impeller, which replaced the earlier Skia-based renderer on iOS and on recent Android versions. The renderer you get depends on your Flutter version and target platform.
- The embedder is the thin platform-specific layer. It creates a window or view, delivers touch and keyboard input, and hands the engine a surface to draw on.
You will mostly work in the top two layers. The lower two explain why a new platform target mainly needs a new embedder, not a rewrite of the framework.
What Dart is
Dart is a statically typed, object-oriented language with a syntax in the C family. Google develops it, and Flutter is its main use. Dart can also run command-line tools and servers. The Dart SDK is bundled inside the Flutter SDK, so installing Flutter gives you Dart as well.
These Dart features matter for Flutter work:
- Sound null safety. A type such as
Stringcan never holdnull. A type such asString?can. The compiler enforces this, so a whole class of null-reference crashes is caught before the app runs. - Async with
Futureandasync/await. Network calls, file reads, and timers return aFuturethat youawait. This keeps the UI from freezing. - Isolates for concurrency. Dart code runs in isolates. Each isolate has its own memory and communicates by passing messages, so isolates don't share mutable state. Your UI code normally runs in one main isolate, and heavy work can move to another.
- Two compilation modes. This is the feature that shapes your daily workflow, and the next section covers it.
Here is a small Dart program that shows null safety, named parameters, and async. Save it as tasks.dart and run dart run tasks.dart. This works once the SDK is installed, which the next chapter covers.
class Task {
Task(this.title, {this.dueInDays});
final String title;
final int? dueInDays; // nullable: a task may have no due date
String describe() {
final days = dueInDays;
if (days == null) {
return '$title (no due date)';
}
return '$title (due in $days days)';
}
}
Future<List<Task>> loadTasks() async {
// Simulates a slow operation such as a network call.
await Future<void>.delayed(const Duration(milliseconds: 100));
return [
Task('Install Flutter', dueInDays: 1),
Task('Run flutter doctor'),
];
}
Future<void> main() async {
final tasks = await loadTasks();
for (final task in tasks) {
print(task.describe());
}
}The output is:
Install Flutter (due in 1 days)
Run flutter doctor (no due date)Note the local variable days. Dart promotes a local variable from int? to int after a null check. It does not do this for fields, because another piece of code could change a field between the check and its use. Copying the field to a local, as above, is the standard fix.
How Dart runs: JIT and AOT
Dart has two ways to run code, and Flutter uses both.
- Just-in-time (JIT). In debug mode, a Dart virtual machine compiles your code as the app runs. It can accept new code into a running app, which is what makes stateful hot reload possible.
- Ahead-of-time (AOT). In release mode, the Dart compiler turns your code into native machine code (ARM or x64) before the app ships. The result starts quickly and runs fast, but it can't accept new code at runtime.
- Web. For the browser, Dart compiles to JavaScript or WebAssembly instead of machine code.
| Mode | Compilation | Speed | Hot reload |
|---|---|---|---|
| Debug | JIT | Slower, with extra checks | Yes |
| Profile | AOT | Close to release, with profiling hooks | No |
| Release | AOT | Fastest, smallest | No |
A later chapter covers build modes in detail. For now, remember the practical result: performance measured in a debug build is not representative. An app that stutters in debug may run smoothly in release.
Widgets: the core idea
In Flutter, almost everything on screen is a widget: a button, a line of text, padding, a layout row, and the whole app. A widget is a lightweight, immutable description of part of the UI. You build a screen by nesting widgets into a tree. Flutter compares successive versions of the tree and updates only what changed on screen.
This is the declarative model. You describe what the UI should look like for the current state, and you don't mutate individual UI objects by hand. When the state changes, you describe the UI again. Later chapters build this out with StatelessWidget, StatefulWidget, and setState.
How Flutter compares to adjacent options
| Approach | How the UI is drawn | Language | Main trade-off |
|---|---|---|---|
| Flutter | Own renderer draws all pixels | Dart | Consistent UI and one codebase, but native controls are imitated |
| Native (Kotlin, Swift) | Platform's own controls | Kotlin, Swift | Best platform fit and immediate access to new APIs, but one codebase per platform |
| React Native | JavaScript drives real native controls | JavaScript or TypeScript | Native controls and a large JavaScript ecosystem, but behavior can differ per platform |
| WebView or hybrid app | A web page inside a native shell | HTML, CSS, JavaScript | Reuses web skills, but weaker native feel |
Flutter is a good fit when you want one team and one codebase to ship a custom-designed app on several platforms. It is a weaker fit when you need heavy use of brand-new platform APIs on release day, or when the app must adopt each platform's native controls automatically. Flutter can still call platform code through platform channels and plugins, but that is extra work.
Common misunderstandings
- "Flutter is a language." It is a framework. The language is Dart.
- "I have to install Dart separately." You don't. The Flutter SDK includes a matching Dart SDK.
- "Flutter apps are interpreted." Release builds are compiled to native machine code, and only debug builds use the JIT virtual machine.
- "Flutter uses native buttons." It draws its own. Material and Cupertino widgets are Flutter's implementations.
- "Debug performance shows the real speed." It does not. Always judge speed on a profile or release build on a real device.
Check your understanding
Exercise
A teammate says, "Flutter apps use the same native button as the operating system, so they always match each platform." Correct this in two or three sentences, and name the Flutter layer that does the drawing.
Show answer
Flutter does not use the operating system's native controls. Its engine, the C++ layer below the Dart framework, draws every pixel with its own renderer onto a surface that the platform embedder provides. Material and Cupertino widgets are Flutter's own imitations of Android and iOS styling, so they can differ slightly from the real native controls.
Exercise
Your app scrolls choppily when you run it with flutter run in debug mode. Explain why this is not enough to conclude the app is slow, and name the Dart compilation mode that release builds use.
Show answer
Debug builds use JIT compilation and include extra checks that make the app slower than it will be for users, so debug timing is not representative. Release builds use ahead-of-time (AOT) compilation to native machine code. You should measure performance in profile or release mode on a real device before deciding there is a problem.
Exercise
In the Dart Task class above, why does describe() copy dueInDays into a local variable days before checking it for null?
Show answer
Dart promotes a local variable from a nullable type (int?) to a non-nullable type (int) after a null check, but it does not promote fields, because other code could change the field after the check. Copying the field into a local variable lets the compiler prove that days is not null in the code after the check.