skills/dart/dart-fix-runtime-errors/SKILL.md
Uses static analysis diagnostics and custom handling patterns for null safety, dynamic lists, contravariance overrides, and unrecoverable errors.
npx skillsauth add dhruvanbhalara/skills dart-fix-runtime-errorsInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Dart enforces a sound type system to ensure that variables match their compile-time types at runtime. To prevent runtime type errors:
covariant keyword to override safety checks.List<dynamic>) directly to strongly typed targets. Always provide explicit generic parameters (e.g. List<String>) during list or map initialization.dynamic values. Use explicit type casting (e.g. as List<Map<String, dynamic>>) only when the underlying structure is guaranteed to match.analysis_options.yaml to enforce explicit casts and catch dynamic typing bugs during code analysis:
analyzer:
language:
strict-casts: true
Properly manage variable states to avoid compile-time analyzer errors and runtime null pointer exceptions:
? to types that are permitted to hold null values. Use ! only when asserting that a value is definitively non-null.late keyword only when the variable is guaranteed to be initialized before its first access. If a late variable is accessed before initialization, a LateInitializationError is thrown at runtime._ (supported in modern Dart versions) to declare local parameters or variables that are intentionally unread, satisfying unused variable lints.Distinguish between recoverable app failures (Exceptions) and unrecoverable bugs (Errors):
Exception (e.g. FormatException, SocketException). These represent expected runtime failures that the application should handle gracefully.Error or its subclasses (e.g. TypeError, RangeError). Errors represent coding bugs that must be corrected during development, not suppressed at runtime.rethrow keyword instead of throwing the caught exception again. rethrow preserves the original stack trace.Follow this checklist to identify, correct, and verify static and runtime bugs:
dart analyze . --fatal-infos in the project root.dart fix --dry-run
dart fix --apply
??, or use the null-aware chain ?..covariant modifier.dart test or flutter test to ensure runtime soundness.TypeError or LateInitializationError occurs at runtime, correcting initialization flows.// Fails Static Analysis due to List<dynamic> assignment:
void printScores(List<int> scores) => print(scores);
void badMain() {
final scores = []; // Inferred as List<dynamic>
scores.add(90);
printScores(scores); // Error: List<dynamic> cannot be assigned to List<int>
}
// Corrected Implementation:
void goodMain() {
final scores = <int>[]; // Explicitly typed
scores.add(90);
printScores(scores); // Compiles and runs safely
}
class Worker {
void performTask(Task t) {}
}
// Fails Static Analysis: Tightening parameter type from Task to CodingTask
class SoftwareEngineer extends Worker {
@override
void performTask(CodingTask t) {}
}
// Corrected Implementation using the covariant keyword:
class LeadSoftwareEngineer extends Worker {
@override
void performTask(covariant CodingTask t) {}
}
class DatabaseClient {
// Late keyword defers null safety checks to runtime
late final String connectionString;
void initialize(String url) {
connectionString = url;
}
void connect() {
// If connect() is called before initialize(), this throws a LateInitializationError
print('Connecting to $connectionString');
}
}
development
Perform REST API networking operations (GET, POST, PUT, DELETE) using the lightweight and robust standard `http` package, including platform configurations and background parsing models.
development
Configure internationalization and localization support using Flutter's built-in l10n system, App Resource Bundle (ARB) files, and ICU formatting syntax.
development
Create model classes with fromJson/toJson using dart:convert and Dart 3 pattern matching. Use when manually mapping JSON to classes, parsing HTTP responses, or choosing between manual and code-generated serialization.
data-ai
Diagnose and fix Flutter layout constraint violations (RenderFlex overflow, unbounded height/width, ParentData misuse). Use when encountering layout exceptions, yellow-black overflow stripes, or red error screens.