Dart fix runtime errors

From flutter/agent-plugins · View interactive version

Dart fix runtime errors is an AI agent skill from the GitHub repository flutter/agent-plugins. 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.

How to use this skill

Skill file

Resolving Dart Static Analysis Errors

Contents

Core Concepts & Guidelines

Type System & Soundness

Enforce Dart's sound type system to prevent runtime invalid states.

Null Safety

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

Error Handling

Distinguish between recoverable exceptions and unrecoverable errors.

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

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:

4. Verify fixes (Feedback Loop)

Run the validator. Review errors. Fix.

dart analyze .
dart test

Examples

Example: Fixing Dynamic List Assignments

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);
}

Example: Fixing Method Overrides (Contravariance)

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
}

Example: Fixing Null Safety with `late`

Input (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';
  }
}
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.