Skip to main content
skill CX Codex

dart-fix-runtime-errors — Claude Code skill: fix Dart runtime errors

Skill automatically catches Dart runtime errors, locates the failing line via LSP and stack trace, applies a fix, and verifies the result through hot reload.

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add dart-lang/skills --skill dart-fix-runtime-errors -a claude-code
Open on GitHub

What dart-fix-runtime-errors does

The skill draws from three sources: it fetches active stack traces via get_runtime_errors, uses LSP to navigate the codebase, and applies fixes with verification through hot reload. This eliminates manual hunting — the agent sees the exact failure point and error context.

The skill handles three main error categories: type mismatches (e.g., assigning List<dynamic> to List<int>), null safety violations (accessing properties on nullable variables without guards), and improper method overrides (contravariance violations). For each, it offers a concrete fix: explicit type annotation, ?. or ?? operators, late keyword, or covariant marker.

The workflow is iterative: run the analyzer, apply automated fixes via dart fix, manually resolve remaining errors, and verify with dart analyze and dart test. If tests fail with TypeError, the skill identifies whether an invalid explicit cast or uninitialized late variable access caused it.

What it helps with

  • Fix type mismatches in Dart code in a single pass
  • Find and resolve null safety violations without reviewing the entire project
  • Apply correct method signatures when overriding in subclasses
  • Automate fix application via dart fix and verify the result

Example prompts for the agent

  • Help me fix Dart errors in this project using dart-fix-runtime-errors
  • Run the analyzer, find runtime errors and apply fixes through hot reload
  • Check collections for type mismatches and add explicit typing where needed

How to install dart-fix-runtime-errors

1Skills CLI (Vercel)

npx skills add dart-lang/skills --skill dart-fix-runtime-errors -a claude-code

2Manually: put the files in place

~/.claude/skills/dart-fix-runtime-errors/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/dart-fix-runtime-errors/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about dart-fix-runtime-errors

How do I install dart-fix-runtime-errors in Claude Code?

Run npx skills add dart-lang/skills --skill dart-fix-runtime-errors -a claude-code, then check ~/.claude/skills/dart-fix-runtime-errors/SKILL.md for configuration details.

What does dart-fix-runtime-errors do?

The skill captures active Dart runtime errors, locates the failing line via LSP and stack trace, applies a fix, and verifies the result through hot reload without restarting the app.

What error types does this skill fix?

The skill targets type mismatches (incorrect collection assignments), null safety violations (accessing nullable variables without checks), improper method overrides, and late initialization issues. For each, it adds explicit types, ?., ??, covariant, or deferred initialization.

Source text

SKILL.md · BSD-3-Clause
---
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
---
# Resolving Dart Static Analysis Errors

## Contents
- [Core Concepts & Guidelines](#core-concepts--guidelines)
  - [Type System & Soundness](#type-system--soundness)
  - [Null Safety](#null-safety)
  - [Error Handling](#error-handling)
- [Workflows](#workflows)
  - [Workflow: Static Analysis Resolution](#workflow-static-analysis-resolution)
- [Examples](#examples)

## 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 `covariant` keyword.
*   **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>`).
*   **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.
*   **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.

### Null Safety
Eliminate static errors related to null safety by correctly managing variable initialization and nullability.

*   **Modifiers:** Apply `?` for nullable types, `!` for null assertions, and `required` for named parameters that cannot be null.
*   **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.
*   **Wildcards:** Use the `_` wildcard variable (Dart 3.7+) for non-binding local variables or parameters to avoid unused variable warnings.

### Error Handling
Distinguish between recoverable exceptions and unrecoverable errors.

*   **Catching:** Catch `Exception` subtypes for recoverable failures.
*   **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.
*   **Rethrowing:** Use `rethrow` inside a `catch` block to propagate an exception while preserving its original stack trace.

## Workflows

### Workflow: Static Analysis Resolution

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.
- [ ] 2. Apply automated fixes.
- [ ] 3. Resolve remaining errors manually.
- [ ] 4. Verify fixes (Feedback Loop).

**1. Run static analyzer**
Execute the Dart analyzer to identify all static errors in the target directory or file.
```bash
dart analyze . --fatal-infos
```

**2. Apply automated fixes**
Use the `dart fix` tool to automatically resolve standard linting and analysis issues.
```bash
# 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:

*   **If the error is a Null Safety issue (e.g., "Property cannot be accessed on a nullable receiver"):**
    *   Verify if the variable can logically be null.
    *   If yes, use optional chaining (`?.`) or provide a fallback (`??`).
    *   If no, and initialization is guaranteed elsewhere, mark the declaration with `late`.
*   **If the error is a Type Mismatch (e.g., "The argument type 'List<dynamic>' can't be assigned..."):**
    *   Trace the variable's initialization.
    *   Add explicit generic type annotations to the instantiation (e.g., `<int>[]` instead of `[]`).
*   **If the error is an Invalid Override (e.g., "The parameter type doesn't match the overridden method"):**
    *   Widen the parameter type to match the superclass, OR
    *   Add the `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.
```bash
dart analyze .
dart test
```
*   **If `dart analyze` reports errors:** Return to Step 3.
*   **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.

## Examples

### Example: Fixing Dynamic List Assignments
**Input (Fails Static Analysis):**
```dart
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):**
```dart
void printInts(List<int> a) => print(a);

void main() {
  final list = <int>[]; // Explicitly typed
  list.add(1);
  list.add(2);
  printInts(list);
}
```

### Example: Fixing Method Overrides (Contravariance)
**Input (Fails Static Analysis):**
```dart
class Animal {
  void chase(Animal a) {}
}

class Cat extends Animal {
  @override
  void chase(Mouse a) {} // Error: Tightening parameter type
}
```

**Output (Passes Static Analysis):**
```dart
class Animal {
  void chase(Animal a) {}
}

class Cat extends Animal {
  @override
  void chase(covariant Mouse a) {} // Explicitly marked covariant
}
```

### Example: Fixing Null Safety with `late`
**Input (Fails Static Analysis):**
```dart
class Thermometer {
  String temperature; // Error: Non-nullable instance field must be initialized

  void read() {
    temperature = '20C';
  }
}
```

**Output (Passes Static Analysis):**
```dart
class Thermometer {
  late String temperature; // Defers initialization check to runtime

  void read() {
    temperature = '20C';
  }
}
```

© dart-lang · BSD-3-Clause · dart-lang/skills

Similar skills

All skills for Programming languages →

From the same set9

dart-add-unit-test Writes Dart and Flutter unit tests with package:test: files under test/, mockito mocks when dependencies get in the way, and a rerun loop until the suite is green. CX Codex +1 19,200installs dart-run-static-analysis Runs dart analyze to catch errors and dart fix --apply to resolve mechanical linter issues. Helps verify Dart code quality during development and before commits. CX Codex +1 19,000installs dart-use-pattern-matching A skill that tells the agent how to use Dart 3 patterns, switch expressions and destructuring on real code: JSON, sealed classes, path segments. CX Codex +1 18,700installs dart-resolve-package-conflicts Skill for resolving Dart package version conflicts when pub get fails. Teaches how to audit dependencies, update packages within constraints, and surgically remove entries from the lockfile. CX Codex +1 18,600installs dart-collect-coverage Collects Dart and Flutter test coverage metrics with the coverage package and generates LCOV reports. Handy for test auditing and CI pipeline preparation. CX Codex +1 18,400installs dart-generate-test-mocks A Dart testing skill: the agent injects external services through constructors, generates mock classes with mockito and build_runner, then writes the unit test. CX Codex +1 18,300installs dart-build-cli-app Skill for building command-line applications in Dart. Provides architectural patterns, entrypoint structure, exit codes, stream routing, and subprocess spawning. CX Codex +1 18,000installs dart-migrate-to-checks-package Migrates Dart test suites from legacy package:matcher to package:checks. Useful when modernizing test assertions for static type safety and detailed diagnostics. CX Codex +1 18,100installs

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor