con un clic
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.
// 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.
Replace the usage of `expect` and similar functions from `package:matcher` to `package:checks` equivalents.
Write and organize unit tests for functions, methods, and classes using `package:test`. Use when creating new logic or fixing bugs to ensure code remains correct and regression-free.
Entrypoint structure, exit codes, cross-platform scripts. Use when building command line utilities, scripts, or applications.
Collect coverage using the coverage packge and create an LCOV report
Define and generate mock objects for external dependencies using `package:mockito` and `build_runner`. Use when unit testing classes that depend on complex external services like APIs or databases.
Workflow for fixing package version conflicts. Use this when `pub get` fails due to incompatible package versions.
| name | dart-fix-runtime-errors |
| description | 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. |
| metadata | {"model":"models/gemini-3.1-pro-preview","last_modified":"Fri, 24 Apr 2026 15:13:22 GMT"} |
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.
dart analyze . --fatal-infos
2. Apply automated fixes
Use the dart fix tool to automatically resolve standard linting and analysis issues.
# Preview changes
dart fix --dry-run
# Apply changes
dart fix --apply
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 .
dart test
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):
void printInts(List<int> a) => print(a);
void main() {
final list = []; // Inferred as List<dynamic>
list.add(1);
list.add(2);
printInts(list); // Error: List<dynamic> can't be assigned to List<int>
}
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);
}
Input (Fails Static Analysis):
class Animal {
void chase(Animal a) {}
}
class Cat extends Animal {
@override
void chase(Mouse a) {} // Error: Tightening parameter type
}
Output (Passes Static Analysis):
class Animal {
void chase(Animal a) {}
}
class Cat extends Animal {
@override
void chase(covariant Mouse a) {} // Explicitly marked covariant
}
lateInput (Fails Static Analysis):
class Thermometer {
String temperature; // Error: Non-nullable instance field must be initialized
void read() {
temperature = '20C';
}
}
Output (Passes Static Analysis):
class Thermometer {
late String temperature; // Defers initialization check to runtime
void read() {
temperature = '20C';
}
}