dart-fix-static-analysis-errors
Workflow for identifying and fixing static analysis errors. Use this after modifying code or if `dart analyze` fails.
SKILL.mdskills/dart/dart-fix-static-analysis-errors/SKILL.md
GitHubContents
- Diagnostic Execution
- Null Safety & Type Resolution
- Flow Analysis & Type Promotion
- Analyzer Configuration
- Workflow: Static Analysis Remediation
- Examples
Diagnostic Execution
Execute the Dart analyzer to identify static errors, warnings, and informational diagnostics across the codebase.
- Run
$ dart analyzeto evaluate all Dart files in the current directory. - Target specific directories or files by appending the path:
$ dart analyze binor$ dart analyze lib/main.dart. - Enforce strictness by failing on info-level issues using the
--fatal-infosflag. - Apply automated quick-fixes for supported diagnostics using
$ dart fix --apply. Preview changes first with$ dart fix --dry-run.
Null Safety & Type Resolution
Address static errors related to Dart's sound null safety and strict type system.
- Nullability: Append
?to types to explicitly allownull(e.g.,String?). - Assertion: Use the postfix bang operator
!to cast a nullable expression to its underlying non-nullable type when you can guarantee it is not null. - Delayed Initialization: Apply the
latemodifier to non-nullable top-level or instance variables that are guaranteed to be initialized before their first read. - Named Parameters: Use the
requiredmodifier for named parameters that do not have a default value and cannot be null. - Explicit Downcasts: If static analysis disallows an implicit downcast, use an explicit cast:
as List<Cat>. - Generic Types: Always provide explicit type annotations to generic classes (e.g.,
List<String>,Map<String, dynamic>).
Flow Analysis & Type Promotion
Leverage Dart's control flow analysis to safely promote nullable types to non-nullable types without manual casting.
- Null Checks: Check a local variable against
null(e.g.,if (value != null)) to automatically promote it. - Type Tests: Use the
isoperator (e.g.,if (value is String)) to promote a variable to a specific subclass. - Early Returns: Use early returns,
break, orthrowto exit a control flow path if a variable is null or the wrong type. - Reachability: Use the
Nevertype for functions that unconditionally throw exceptions or terminate the process.
Analyzer Configuration
Configure the analysis_options.yaml file at the package root.
- Enable
strict-casts: trueto prevent implicit downcasts fromdynamic. - Enable
strict-inference: trueto prevent the analyzer from falling back todynamic. - Enable
strict-raw-types: trueto require explicit type arguments on generic types. - Suppress specific diagnostics using
// ignore_for_file:or// ignore:.
Workflow: Static Analysis Remediation
Task Progress Checklist
- Run
$ dart analyzeto establish a baseline of errors. - Run
$ dart fix --applyto resolve automatically fixable issues. - Address remaining Null Safety errors (
?,!,late,required). - Address remaining Type System errors (explicit
ascasts, generic type annotations). - Run
$ dart analyzeto verify all errors are resolved. - Execute tests to ensure fixes did not introduce runtime exceptions.
Feedback Loop
- Run Validator:
$ dart analyze - Review Errors: Identify the file, line number, and diagnostic code.
- Fix: Apply the appropriate null safety or type resolution fix.
- Repeat: Continue until
$ dart analyzereturns "No issues found!".
Type Promotion via Local Variable Assignment
// Incorrect (Fails Analysis):
class Coffee {
String? _temperature;
void checkTemp() {
if (_temperature != null) {
print(_temperature.length); // ERROR
}
}
}
// Correct:
class Coffee {
String? _temperature;
void checkTemp() {
final temp = _temperature; // Copy to local variable
if (temp != null) {
print(temp.length); // SUCCESS
}
}
}
Strict Analyzer Configuration
include: package:lints/recommended.yaml
analyzer:
language:
strict-casts: true
strict-inference: true
strict-raw-types: true
errors:
invalid_assignment: error
missing_return: error
dead_code: info