flutter
type_error
ai_generated
true
type 'Null' is not a subtype of type 'String' in type cast
ID: flutter/type-cast-error
87%Fix Rate
89%Confidence
3Evidence
2023-01-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3 | active | — | — | — |
Root Cause
Runtime type cast failure. A value (often null or wrong type) is being cast to an incompatible type, frequently from JSON parsing or API responses.
genericWorkarounds
-
92% success Use safe parsing with null-aware operators and default values
final name = json['name'] as String? ?? 'unknown'; // safe cast with fallback
Sources: https://api.flutter.dev/
-
88% success Use fromJson factory constructors with explicit type handling
factory User.fromJson(Map<String, dynamic> json) => User(name: json['name']?.toString() ?? '');
-
82% success Validate API response structure before casting
if (json case {'name': String name, 'age': int age}) { ... } // Dart 3 pattern matching
Dead Ends
Common approaches that don't work:
-
Cast with 'as' keyword everywhere to force types
80% fail
'as' throws at runtime when the cast fails; it does not convert types, only asserts them
-
Disable null safety to avoid type cast errors
90% fail
Disabling null safety removes all compile-time type checking; makes ALL type issues silent runtime failures