VOOZH about

URL: https://dev.to/edwin_gichira_92748e19bb6/flutter-architecture-explained-what-every-beginner-needs-to-know-1oim

⇱ Flutter Architecture Explained: What Every Beginner Needs to Know - DEV Community


πŸ‘

If you're just getting started with Flutter, you'll write your first widget within minutes. But somewhere around day three, you'll hit a wall β€” why does context crash here? Why did my whole screen rebuild when I changed one variable? Why does Flutter feel so different from React Native or SwiftUI?

The answer is architecture. Once you understand how Flutter actually works under the hood, everything else clicks into place. This is Part 1 of the Flutter From Zero series, and we're covering the foundations.


What Is Flutter β€” and Why Does It Exist?

Flutter is an open-source UI toolkit from Google that lets you build natively compiled apps for six platforms from a single Dart codebase: Android, iOS, Web, Windows, macOS, and Linux.

But here's what makes it different from every other cross-platform tool:

Flutter doesn't use native UI components. It draws its own UI using a custom rendering engine.

That's a big deal. Tools like React Native translate your code into native components β€” so a <Button> becomes an Android Button or an iOS UIButton. Flutter skips that entirely and paints pixels directly to the screen using its own engine (Skia or the newer Impeller). The result: pixel-perfect UI that looks identical on every platform, and no JavaScript bridge slowing things down.

Problem Flutter's Solution
Separate iOS/Android codebases One Dart codebase for all platforms
Bridge overhead (React Native) No bridge β€” renders directly
Inconsistent UI per platform Flutter draws its own widgets
Slow cross-platform performance Compiles to native ARM machine code

Flutter 1.0 shipped in December 2018. By Flutter 3.x, it targets six platforms stably.


The 3 Layers of Flutter

Flutter's architecture is a three-layer sandwich. Each layer depends on the one below it. You work in the top layer and rarely touch the others β€” but knowing what they do changes how you debug and optimize.

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ FRAMEWORK LAYER (Dart) β”‚
β”‚ Material / Cupertino / Widgets / ... β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ ENGINE LAYER (C++) β”‚
β”‚ Skia / Impeller / Dart Runtime β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ EMBEDDER LAYER (Platform) β”‚
β”‚ Android / iOS / Windows / macOS ... β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

1. Framework Layer (Dart)

This is where you spend 95% of your time. It's written entirely in Dart and contains everything you interact with: widgets, routing, theming, gestures, and animations.

The sub-layers stack like this, bottom to top:

Material / Cupertino ← Design-system widgets (buttons, dialogs, etc.)
Widgets ← Core primitives (StatelessWidget, StatefulWidget)
Rendering ← RenderObjects, layout, paint
Animation / Painting ← Curves, tweens, Canvas API
Foundation ← Utilities, diagnostics, change notifiers

2. Engine Layer (C++)

The engine is Flutter's heart. You never write C++, but this layer is doing the heavy lifting:

  • Skia / Impeller β€” renders pixels to the screen
  • Dart Runtime β€” runs your Dart code (AOT in release builds, JIT in debug)
  • Platform Channels β€” lets Dart talk to native code
  • Text Layout β€” handles fonts and text shaping

Quick note on Impeller: it's Flutter's newer renderer, designed to eliminate shader-compilation jank (that stutter you feel the first time a new animation runs). It's now the default on iOS and rolling out on Android.

3. Embedder Layer (Platform-specific)

The embedder is what makes Flutter runnable on a given OS. It's written in the host platform's language (Kotlin for Android, Swift for iOS, C++ for Desktop).

Its job: create a Flutter Engine instance, give it a surface to draw on, and feed it input events (touch, keyboard, mouse). Because this layer is separated out, Flutter can run on custom hardware β€” anyone can write a custom embedder.


The 3 Trees and the Rendering Pipeline

This is the part that trips up most beginners. Flutter manages three parallel trees to render your UI efficiently.

Your Code
 β”‚
 β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Widget Tree β”‚ ← Immutable blueprints (cheap to create)
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
 β”‚ inflate
 β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Element Treeβ”‚ ← Mutable, long-lived (lifecycle manager)
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
 β”‚ creates
 β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Render Tree β”‚ ← Does the actual layout and painting (expensive)
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Widget Tree β€” Widgets are immutable Dart objects. Think of them as configuration blueprints. Every time build() runs, new widgets are created. This sounds wasteful, but widgets are intentionally cheap and lightweight.

Element Tree β€” Elements are long-lived and mutable. When Flutter first renders a widget, it creates an Element for it. That Element persists across rebuilds β€” it holds the connection between the widget (config) and the render object (output).

When you call setState(), Flutter marks the Element dirty and schedules a rebuild. It does NOT recreate the Element or RenderObject from scratch unless the widget type changes.

Render Tree β€” RenderObjects do the real work: computing sizes, calculating positions, and painting pixels. This tree is expensive to create, so Flutter reuses RenderObjects as aggressively as possible.

The Render Pipeline

Each frame goes through five stages:

Build β†’ Layout β†’ Paint β†’ Composite β†’ Rasterize
  1. Build β€” build() is called on dirty widgets; new widget nodes are produced
  2. Layout β€” RenderObjects compute sizes and positions using constraints
  3. Paint β€” RenderObjects draw onto a Canvas
  4. Composite β€” Layers are assembled into a scene
  5. Rasterize β€” The GPU converts the scene to pixels on screen

Flutter targets 60fps (16.6ms per frame) or 120fps on capable hardware. The three-tree model is what makes that possible β€” only dirty parts of the tree get rebuilt, not the entire UI.


Project Structure

Running flutter create my_app produces this layout:

my_app/
β”œβ”€β”€ lib/ ← Your Dart code lives here
β”‚ └── main.dart ← Entry point
β”œβ”€β”€ android/ ← Android embedder + native code
β”œβ”€β”€ ios/ ← iOS embedder + native code
β”œβ”€β”€ web/ ← Web embedder
β”œβ”€β”€ windows/
β”œβ”€β”€ macos/
β”œβ”€β”€ linux/
β”œβ”€β”€ assets/ ← Images, fonts, JSON, etc.
β”œβ”€β”€ test/ ← Unit and widget tests
└── pubspec.yaml ← Project manifest

A recommended structure for real apps inside lib/:

lib/
β”œβ”€β”€ main.dart
β”œβ”€β”€ app.dart ← Root widget + routing
β”œβ”€β”€ features/ ← Feature-based folders
β”‚ β”œβ”€β”€ home/
β”‚ └── auth/
β”œβ”€β”€ shared/ ← Reusable widgets, utils
└── data/ ← Models, APIs, repositories

pubspec.yaml

This is your project's manifest β€” it declares dependencies, assets, and fonts:

name: my_app
version: 1.0.0+1

environment:
 sdk: ">=3.0.0<4.0.0"

dependencies:
 flutter:
 sdk: flutter
 http: ^1.2.0

flutter:
 assets:
 - assets/images/
 fonts:
 - family: Poppins
 fonts:
 - asset: assets/fonts/Poppins-Regular.ttf
 - asset: assets/fonts/Poppins-Bold.ttf
 weight: 700

After any edit to pubspec.yaml, run:

flutter pub get

App Lifecycle

Flutter exposes lifecycle events via WidgetsBindingObserver. Understanding this is critical when you work with cameras, audio, network connections, or local storage.

The States

detached β†’ inactive β†’ resumed ↔ hidden / paused β†’ detached
  • resumed β€” app is fully active in the foreground βœ…
  • inactive β€” visible but not receiving input (e.g., incoming call overlay)
  • hidden β€” not visible but not suspended (added in Flutter 3.13)
  • paused β€” fully backgrounded / suspended
  • detached β€” engine alive, no view attached (startup or shutdown)

Listening to Lifecycle Events

class _MyScreenState extends State<MyScreen> with WidgetsBindingObserver {

 @override
 void initState() {
 super.initState();
 WidgetsBinding.instance.addObserver(this);
 }

 @override
 void dispose() {
 WidgetsBinding.instance.removeObserver(this); // ALWAYS unregister
 super.dispose();
 }

 @override
 void didChangeAppLifecycleState(AppLifecycleState state) {
 switch (state) {
 case AppLifecycleState.resumed:
 // Resume camera, refresh data
 break;
 case AppLifecycleState.paused:
 // Save state, release resources
 break;
 default:
 break;
 }
 }
}

BuildContext β€” The Part That Confuses Everyone

BuildContext is a handle to your widget's location in the Widget Tree. Under the hood, it's the Element itself.

@override
Widget build(BuildContext context) {
 final theme = Theme.of(context); // walks UP the tree to find ThemeData
 final size = MediaQuery.of(context).size;
 return Container(color: theme.colorScheme.primary);
}

When you call Theme.of(context), Flutter walks up the Element Tree to find the nearest Theme ancestor. Your widget then subscribes to that theme β€” it rebuilds automatically when the theme changes.

The 4 Mistakes to Avoid

❌ Using context across an async gap

// WRONG
onPressed: () async {
 await Future.delayed(Duration(seconds: 2));
 Navigator.of(context).pop(); // context may be stale!
}

// CORRECT
onPressed: () async {
 await Future.delayed(Duration(seconds: 2));
 if (!mounted) return; // guard check first
 Navigator.of(context).pop();
}

❌ Using context in initState()

// WRONG β€” widget isn't in the tree yet
@override
void initState() {
 super.initState();
 final theme = Theme.of(context); // CRASH
}

// CORRECT β€” use didChangeDependencies() instead
@override
void didChangeDependencies() {
 super.didChangeDependencies();
 final theme = Theme.of(context); // βœ…
}

❌ Wrong context level for Scaffold

// CORRECT β€” use Builder to get a context below the Scaffold
return Scaffold(
 body: Builder(
 builder: (innerContext) {
 return ElevatedButton(
 onPressed: () {
 ScaffoldMessenger.of(innerContext).showSnackBar(...); // βœ…
 },
 );
 },
 ),
);

❌ Storing context as a field

// WRONG β€” stored context goes stale after rebuilds or navigation
BuildContext? savedContext;

Quick Cheat Sheet

Flutter Architecture
β”‚
β”œβ”€β”€ 3 Layers
β”‚ β”œβ”€β”€ Framework (Dart) β€” what you write
β”‚ β”œβ”€β”€ Engine (C++) β€” renders, runs Dart
β”‚ └── Embedder (Native) β€” platform window/surface
β”‚
β”œβ”€β”€ 3 Trees
β”‚ β”œβ”€β”€ Widget Tree β€” immutable blueprints
β”‚ β”œβ”€β”€ Element Tree β€” mutable, manages lifecycle
β”‚ └── Render Tree β€” layout + paint (expensive)
β”‚
β”œβ”€β”€ Render Pipeline
β”‚ Build β†’ Layout β†’ Paint β†’ Composite β†’ Rasterize
β”‚
β”œβ”€β”€ Lifecycle
β”‚ detached β†’ inactive β†’ resumed ↔ paused/hidden
β”‚
└── BuildContext Rules
 βœ… Use in build(), didChangeDependencies()
 βœ… Check mounted after async gaps
 ❌ Never in initState() directly
 ❌ Never store long-term

What's Next?

In Part 2, we'll dive into StatelessWidget vs StatefulWidget, when to use each, and how state actually flows through a Flutter app.

If this helped you, drop a ❀️ or leave a comment with questions β€” I read every one. Follow the Flutter From Zero series so you don't miss the next post.


Written for Flutter 3.x | Dart SDK 3.0+