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.md

GitHub

Type 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 covariant keyword.
  • Generics & Collections: Add explicit type annotations to generic classes (e.g., List<T>, Map<K, V>). Never assign a List<dynamic> to a typed list.
  • Downcasting: Avoid implicit downcasts from dynamic. Use explicit casts when necessary.
  • Strict Casts: Enable strict-casts: true in analysis_options.yaml.

Null Safety

  • Modifiers: Apply ? for nullable types, ! for null assertions, and required for named parameters.
  • Late Initialization: Use the late keyword 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 Exception subtypes for recoverable failures.
  • Errors: Never explicitly catch Error or its subtypes. Enable avoid_catching_errors linter rule.
  • Rethrowing: Use rethrow inside a catch block to preserve stack trace.

Task Progress:

    1. Run static analyzer: dart analyze . --fatal-infos
    1. Apply automated fixes: dart fix --apply
    1. Resolve remaining errors manually.
    1. Verify fixes: dart analyze . and dart test

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