dart-fix-runtime-errors
Resolving Dart Static Analysis Errors
Contents
Core Concepts & Guidelines
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
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 (e.g.,List<Cat>). - Downcasting: Avoid implicit downcasts from
dynamic. Use explicit casts (e.g.,as List<Cat>) when necessary, but ensure the underlying runtime type matches to preventTypeErrorexceptions. - Strict Casts: Enable
strict-casts: trueinanalysis_options.yamlunderanalyzer: language:to force explicit casting and catch implicit downcast errors at compile time.
More from dart-lang/skills
dart-add-unit-test
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.
2.1Kdart-run-static-analysis
Execute `dart analyze` to identify warnings and errors, and use `dart fix --apply` to automatically resolve mechanical lint issues. Use during development to ensure code quality and before committing changes.
2.1Kdart-use-pattern-matching
Use switch expressions and pattern matching where appropriate
2.0Kdart-resolve-package-conflicts
Workflow for fixing package version conflicts. Use this when `pub get` fails due to incompatible package versions.
2.0Kdart-collect-coverage
Collect coverage using the coverage packge and create an LCOV report
2.0Kdart-generate-test-mocks
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.
2.0K