Master Dart's static type system, type inference, and sound null safety guarantees. Understand flow analysis, type promotion, null-aware operators, and the late initialization contract.
Dart is a strongly typed, statically checked programming language featuring sound null safety.
Soundness Guarantee: If the compiler determines that an expression has a non-nullable type (e.g. String), the runtime environment guarantees that this expression can never evaluate to null under any execution path.
This eliminates the entire category of runtime NullPointerException / NullReferenceException crashes common in languages without sound null safety.
// Non-nullable by default:String title = "Flutter in Action";// title = null; // ❌ Compile-time Error: A value of type 'Null' can't be assigned to 'String'// Nullable opt-in using the '?' suffix:String? subtitle; // ✅ Holds null by default or a valid Stringsubtitle = "Advanced Architecture";subtitle = null; // ✅ Valid assignment
When you declare a widget as const, Flutter reuses the exact same in-memory object instance across widget rebuilds. The Element tree recognizes identical object references (identical(oldWidget, newWidget) == true) and skips calling build() for that subtree entirely!
Dart's compiler incorporates sophisticated Control Flow Analysis. When you check a nullable variable against null, the compiler automatically promotes the variable from T? to T within that scope:
void displayUserInfo(String? username, List<int>? scores) { // Before check: username is String? if (username != null) { // Inside this block: username is promoted to non-nullable String! print("User: ${username.toUpperCase()} (Length: ${username.length})"); } // Type promotion with early return: if (scores == null || scores.isEmpty) { return; } // scores is promoted to non-nullable List<int> here print("Highest score: ${scores.first}");}
[!WARNING]
Field Promotion Limitation: Class member fields (e.g. this.user) cannot always be automatically promoted because getter methods or subclass overrides might return null between accesses. To promote a class field, assign it to a local variable:
The late keyword informs the compiler: "Trust me, this variable will be initialized before I ever read it."
class ProfileScreenState extends State<ProfileScreen> { // Valid use-case: Initialized in initState() where BuildContext or configuration is ready late final AnimationController _controller; late final String _userIdentifier; @override void initState() { super.initState(); _userIdentifier = widget.userId; _controller = AnimationController(vsync: this, duration: const Duration(seconds: 1)); } @override void dispose() { _controller.dispose(); super.dispose(); }}
[!CAUTION]
If you access an uninitialized late variable, Dart throws a runtime LateInitializationError. Never use late as a lazy workaround for nullable API responses—use nullable types (String?) for values that might truly be missing.