Python Type Promotion & Flow Analysis
Dart features sophisticated **Flow Analysis**. When the compiler sees a control flow branch that checks `if (variable != null)` or `if (variable is Subclass)`, it **promotes** the variable from a nullable or general type to a non-nullable or specific type automatically throughout that branch. In Dart 3.2+, type promotion even extends to private final fields.
"Type promotion is like getting an authenticated wristband at a conference: once the check-in desk verifies your ID at the door (`!= null`), you can walk into every VIP room inside without showing your passport again."
Deep Dive: How It Works
Null Checking Promotion: `String? x` checked with `if (x != null)` is automatically treated as `String` inside the block.
Type Test Promotion: `Object obj` checked with `if (obj is List)` is automatically promoted to `List` (unlocking `.length`, `.add()`).
Definite Assignment: Flow analysis verifies that variables are assigned before use along every possible execution path.
Syntax Blueprint
void process(Object? item) {
if (item is String) {
print(item.toUpperCase()); // Promoted to String!
} else if (item is List) {
print("Items: ${item.length}"); // Promoted to List!
}
}Smart type promotion across branching flow conditions.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Expecting public mutable fields to promote across methods.Why it happens: A public mutable field could theoretically be modified by another thread or getter between checks.
How to fix: Copy the field to a local variable (`final val = myField; if (val != null) ...`).
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Promote Object to String
Write function `void printUpper(Object? val) { if (val is String) { print(val.toUpperCase()); } }`.
In `main()`, call `printUpper("zencompiler");`.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.