npx skills add ...
npx skills add dart-lang/skills --skill dart-fix-runtime-errors
npx skills add dart-lang/skills --skill 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.
The same skill content is published under more than one repo. The install counts are split across them; any of these commands works.
Enforce Dart's sound type system to prevent runtime invalid states.
covariant keyword.List<T>, Map<K, V>). Never assign a List<dynamic> to a typed list (e.g., List<Cat>).dynamic. Use explicit casts (e.g., as List<Cat>) when necessary, but ensure the underlying runtime type matches to prevent TypeError exceptions.strict-casts: true in analysis_options.yaml under analyzer: language: to force explicit casting and catch implicit downcast errors at compile time.Eliminate static errors related to null safety by correctly managing variable initialization and nullability.
? for nullable types, ! for null assertions, and required for named parameters that cannot be null.late keyword for non-nullable variables guaranteed to be initialized before use. Apply this specifically to top-level or instance variables where Dart's control flow analysis cannot definitively prove initialization._ wildcard variable (Dart 3.7+) for non-binding local variables or parameters to avoid unused variable warnings.Distinguish between recoverable exceptions and unrecoverable errors.
Exception subtypes for recoverable failures.Error or its subtypes (e.g., TypeError, ArgumentError). Errors indicate programming bugs that must be fixed, not caught. Enforce this by enabling the avoid_catching_errors linter rule.rethrow inside a catch block to propagate an exception while preserving its original stack trace.Use this sequential workflow to identify, fix, and verify static analysis errors in a Dart project. Copy the checklist to track your progress.
Task Progress:
1. Run static analyzer Execute the Dart analyzer to identify all static errors in the target directory or file.
2. Apply automated fixes
Use the dart fix tool to automatically resolve standard linting and analysis issues.
3. Resolve remaining errors manually Review the remaining analyzer output and apply conditional logic based on the error type:
?.) or provide a fallback (??).late.<int>[] instead of []).covariant keyword to the parameter if tightening the type is intentionally required by the domain logic.4. Verify fixes (Feedback Loop) Run the validator. Review errors. Fix.
dart analyze reports errors: Return to Step 3.dart test fails with a TypeError: You have introduced an invalid explicit cast (as T) or accessed an uninitialized late variable. Locate the runtime failure and correct the type hierarchy or initialization order.Input (Fails Static Analysis):
Output (Passes Static Analysis):
Input (Fails Static Analysis):
Output (Passes Static Analysis):
lateInput (Fails Static Analysis):
Output (Passes Static Analysis):
void printInts(List<int> a) => print(a);
void main() {
final list = <int>[]; // Explicitly typed
list.add(1);
list.add(2);
printInts(list);
}class Animal {
void chase(Animal a) {}
}
class Cat extends Animal {
@override
void chase(Mouse a) {} // Error: Tightening parameter type
}class Animal {
void chase(Animal a) {}
}
class Cat extends Animal {
@override
void chase(covariant Mouse a) {} // Explicitly marked covariant
}class Thermometer {
String temperature; // Error: Non-nullable instance field must be initialized
void read() {
temperature = '20C';
}
}class Thermometer {
late String temperature; // Defers initialization check to runtime
void read() {
temperature = '20C';
}
}