dart-fix-runtime-errors
Uses get_runtime_errors and lsp to fetch an active stack trace, locate the failing line, apply a fix, and verify resolution via hot_reload.
SKILL.mdskills/dart/dart-fix-runtime-errors/SKILL.md
GitHubType System & Soundness
Enforce Dart's sound type system to prevent runtime invalid states.
- Method Overrides: Maintain sound return types (covariant) and parameter types (contravariant). Never tighten a parameter type in a subclass unless explicitly marked with the
covariantkeyword. - Generics & Collections: Add explicit type annotations to generic classes (e.g.,
List<T>,Map<K, V>). Never assign aList<dynamic>to a typed list. - Downcasting: Avoid implicit downcasts from
dynamic. Use explicit casts when necessary. - Strict Casts: Enable
strict-casts: trueinanalysis_options.yaml.
Null Safety
- Modifiers: Apply
?for nullable types,!for null assertions, andrequiredfor named parameters. - Late Initialization: Use the
latekeyword for non-nullable variables guaranteed to be initialized before use. - Wildcards: Use the
_wildcard variable (Dart 3.7+) for non-binding local variables.
Error Handling
- Catching: Catch
Exceptionsubtypes for recoverable failures. - Errors: Never explicitly catch
Erroror its subtypes. Enableavoid_catching_errorslinter rule. - Rethrowing: Use
rethrowinside acatchblock to preserve stack trace.
Task Progress:
-
- Run static analyzer:
dart analyze . --fatal-infos
- Run static analyzer:
-
- Apply automated fixes:
dart fix --apply
- Apply automated fixes:
-
- Resolve remaining errors manually.
-
- Verify fixes:
dart analyze .anddart test
- Verify fixes:
Example: Fixing Dynamic List Assignments
// Before (fails):
final list = []; // List<dynamic>
printInts(list); // Error
// After (passes):
final list = <int>[]; // Explicitly typed
printInts(list);
Example: Fixing Method Overrides
// Before: void chase(Mouse a) {} // Error
// After: void chase(covariant Mouse a) {} // OK
Example: Fixing Null Safety with late
// Before: String temperature; // Error
// After: late String temperature; // OK