$npx -y skills add flutter/agent-plugins --skill dart-fix-runtime-errorsUses get_runtime_errors and lsp to fetch an active stack trace, locate the failing line, apply a fix, and verify resolution via hot_reload.
| 1 | # Resolving Dart Static Analysis Errors |
| 2 | |
| 3 | ## Contents |
| 4 | - [Core Concepts & Guidelines](#core-concepts--guidelines) |
| 5 | - [Type System & Soundness](#type-system--soundness) |
| 6 | - [Null Safety](#null-safety) |
| 7 | - [Error Handling](#error-handling) |
| 8 | - [Workflows](#workflows) |
| 9 | - [Workflow: Static Analysis Resolution](#workflow-static-analysis-resolution) |
| 10 | - [Examples](#examples) |
| 11 | |
| 12 | ## Core Concepts & Guidelines |
| 13 | |
| 14 | ### Type System & Soundness |
| 15 | Enforce Dart's sound type system to prevent runtime invalid states. |
| 16 | |
| 17 | * **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. |
| 18 | * **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 (e.g., `List<Cat>`). |
| 19 | * **Downcasting:** Avoid implicit downcasts from `dynamic`. Use explicit casts (e.g., `as List<Cat>`) when necessary, but ensure the underlying runtime type matches to prevent `TypeError` exceptions. |
| 20 | * **Strict Casts:** Enable `strict-casts: true` in `analysis_options.yaml` under `analyzer: language:` to force explicit casting and catch implicit downcast errors at compile time. |
| 21 | |
| 22 | ### Null Safety |
| 23 | Eliminate static errors related to null safety by correctly managing variable initialization and nullability. |
| 24 | |
| 25 | * **Modifiers:** Apply `?` for nullable types, `!` for null assertions, and `required` for named parameters that cannot be null. |
| 26 | * **Late Initialization:** Use the `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. |
| 27 | * **Wildcards:** Use the `_` wildcard variable (Dart 3.7+) for non-binding local variables or parameters to avoid unused variable warnings. |
| 28 | |
| 29 | ### Error Handling |
| 30 | Distinguish between recoverable exceptions and unrecoverable errors. |
| 31 | |
| 32 | * **Catching:** Catch `Exception` subtypes for recoverable failures. |
| 33 | * **Errors:** Never explicitly catch `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. |
| 34 | * **Rethrowing:** Use `rethrow` inside a `catch` block to propagate an exception while preserving its original stack trace. |
| 35 | |
| 36 | ## Workflows |
| 37 | |
| 38 | ### Workflow: Static Analysis Resolution |
| 39 | |
| 40 | Use this sequential workflow to identify, fix, and verify static analysis errors in a Dart project. Copy the checklist to track your progress. |
| 41 | |
| 42 | **Task Progress:** |
| 43 | - [ ] 1. Run static analyzer. |
| 44 | - [ ] 2. Apply automated fixes. |
| 45 | - [ ] 3. Resolve remaining errors manually. |
| 46 | - [ ] 4. Verify fixes (Feedback Loop). |
| 47 | |
| 48 | **1. Run static analyzer** |
| 49 | Execute the Dart analyzer to identify all static errors in the target directory or file. |
| 50 | ```bash |
| 51 | dart analyze . --fatal-infos |
| 52 | ``` |
| 53 | |
| 54 | **2. Apply automated fixes** |
| 55 | Use the `dart fix` tool to automatically resolve standard linting and analysis issues. |
| 56 | ```bash |
| 57 | # Preview changes |
| 58 | dart fix --dry-run |
| 59 | # Apply changes |
| 60 | dart fix --apply |
| 61 | ``` |
| 62 | |
| 63 | **3. Resolve remaining errors manually** |
| 64 | Review the remaining analyzer output and apply conditional logic based on the error type: |
| 65 | |
| 66 | * **If the error is a Null Safety issue (e.g., "Property cannot be accessed on a nullable receiver"):** |
| 67 | * Verify if the variable can logically be null. |
| 68 | * If yes, use optional chaining (`?.`) or provide a fallback (`??`). |
| 69 | * If no, and initialization is guaranteed elsewhere, mark the declaration with `late`. |
| 70 | * **If the error is a Type Mismatch (e.g., "The argument type 'List<dynamic>' can't be assigned..."):** |
| 71 | * Trace the variable's initialization. |
| 72 | * Add explicit generic type annotations to the instantiation (e.g., `<int>[]` instead of `[]`). |
| 73 | * **If the error is an Invalid Override (e.g., "The parameter type doesn't match the overridden method"):** |
| 74 | * Widen the parameter type to match the superclass, OR |
| 75 | * Add the `covariant` keyword to the parameter if tightening the type is intentionally required by the domain logic. |
| 76 | |
| 77 | **4. Verify fixes (Feedback Loop)** |
| 78 | Run the validator. Review errors. Fix. |
| 79 | ```bash |
| 80 | dart analyze . |
| 81 | dart test |
| 82 | ``` |
| 83 | * **If `dart analyze` reports errors:** Return to Step 3. |
| 84 | * **If `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. |
| 85 | |
| 86 | ## Examples |
| 87 | |
| 88 | ### Example: Fixing Dynamic List Assignments |
| 89 | **Input (Fails Static Analysis):** |
| 90 | ```dart |
| 91 | void printInts(List<int> a) => pri |