fix_flutter_deprecations 0.1.2 copy "fix_flutter_deprecations: ^0.1.2" to clipboard
fix_flutter_deprecations: ^0.1.2 copied to clipboard

A powerful and extensible Dart CLI tool that automatically fixes Flutter deprecations in your codebase

Fix Flutter Deprecations #

Fix Flutter Deprecations Logo

ci coverage style: very good analysis License: MIT

A powerful and extensible Dart command-line tool that automatically fixes Flutter deprecations in your codebase. As Flutter evolves, APIs get deprecated and replaced with new ones. This tool helps you migrate your codebase efficiently by automatically applying common deprecation fixes.

Fix Flutter Deprecations in action

Features ✨ #

  • Automatic Deprecation Fixes: Automatically updates deprecated Flutter APIs to their modern equivalents
  • Intelligent Code Transformation: Smart handling of complex widget migrations like WillPopScope β†’ PopScope
  • Lint Rule Fixes: Automatically resolves common linting issues like use_build_context_synchronously
  • Extensible Architecture: Easily add new deprecation rules as Flutter evolves
  • Safe Operation: Dry-run mode to preview changes before applying them
  • Selective Fixes: Apply specific deprecation fixes or all at once (6+ rules available)
  • Progress Tracking: Clear feedback on what's being changed
  • Backup Support: Optional backup creation before making changes

Currently Supported Deprecations #

Rule Deprecated API Replacement Flutter Version
withOpacity .withOpacity(value) .withValues(alpha: value) 3.27+
surfaceContainerHighest surfaceVariant surfaceContainerHighest Material 3
onSurface onSurfaceVariant onSurface Material 3
willPopScope WillPopScope PopScope 3.12+
multipleUnderscores Multiple underscores (__identifier) Single underscore (_identifier) Linting
buildContextAsync Unsafe BuildContext after async Added mounted checks Linting

Detailed Rule Descriptions #

🎨 Flutter Widget Deprecations

  • willPopScope: Converts deprecated WillPopScope to PopScope with intelligent callback transformation
    • Simple boolean returns β†’ canPop property
    • Complex logic β†’ onPopInvoked callbacks with proper navigation handling

πŸ”§ Linting Rule Fixes

  • multipleUnderscores: Fixes "unnecessary use of multiple underscores" warnings

    • Preserves generated code patterns and test mocks
    • Converts __identifier to _identifier where appropriate
  • buildContextAsync: Fixes use_build_context_synchronously warnings

    • Detects BuildContext usage after async operations
    • Adds if (mounted) checks for StatefulWidget
    • Adds if (context.mounted) checks for other contexts
    • Supports Navigator, showDialog, ScaffoldMessenger, Theme, and MediaQuery operations

Installation πŸ“¦ #

Global Installation #

dart pub global activate fix_flutter_deprecations

Local Development #

dart pub global activate --source=path .

Usage πŸš€ #

Fix all deprecations in your project #

fix_deprecations

Preview changes without applying them (dry run) #

fix_deprecations --dry-run

Apply specific deprecation fixes #

fix_deprecations --rules withOpacity,willPopScope,buildContextAsync

Fix a specific file or directory #

fix_deprecations lib/src/widgets/
fix_deprecations lib/main.dart

Create backups before fixing #

fix_deprecations --backup

List all available deprecation rules #

fix_deprecations --list-rules

Command Reference #

# Fix all deprecations in current directory
fix_deprecations

# Fix with specific options
fix_deprecations --dry-run --verbose
fix_deprecations --rules withOpacity,willPopScope --backup lib/

# List available deprecation rules
fix_deprecations --list-rules

# Show version
fix_deprecations --version

# Show help
fix_deprecations --help

Examples πŸ’‘ #

WillPopScope to PopScope Migration #

Before:

WillPopScope(
  onWillPop: () async {
    return await showDialog(...);
  },
  child: Scaffold(...),
)

After:

PopScope(
  canPop: false,
  onPopInvoked: (bool didPop) async {
    if (didPop) return;
    final NavigatorState navigator = Navigator.of(context);
    final bool shouldPop = await showDialog(...);
    if (shouldPop) navigator.pop();
  },
  child: Scaffold(...),
)

BuildContext Async Safety #

Before:

Future<void> _handleSubmit() async {
  await _submitForm();
  Navigator.of(context).pop(); // ⚠️ use_build_context_synchronously
}

After:

Future<void> _handleSubmit() async {
  await _submitForm();
  if (mounted) {
    Navigator.of(context).pop(); // βœ… Safe to use
  }
}

Multiple Underscores Fix #

Before:

class MyClass {
  String __privateField; // ⚠️ unnecessary multiple underscores
}

After:

class MyClass {
  String _privateField; // βœ… Single underscore
}

Running Tests with coverage πŸ§ͺ #

To run all unit tests use the following command:

$ dart pub global activate coverage 1.2.0
$ dart test --coverage=coverage
$ dart pub global run coverage:format_coverage --lcov --in=coverage --out=coverage/lcov.info

To view the generated coverage report you can use lcov .

# Generate Coverage Report
$ genhtml coverage/lcov.info -o coverage/

# Open Coverage Report
$ open coverage/index.html

Adding New Deprecation Rules πŸ”§ #

The tool is designed to be easily extensible. To add a new deprecation rule:

  1. Create a new rule class in lib/src/rules/
  2. Implement the DeprecationRule interface
  3. Register the rule in the rule registry

Example:

class MyDeprecationRule extends DeprecationRule {
  @override
  String get name => 'myDeprecation';
  
  @override
  String get description => 'Fixes MyOldAPI to MyNewAPI';
  
  @override
  String apply(String content) {
    // Implementation here
  }
}

Architecture πŸ—οΈ #

The project follows a clean, extensible architecture:

  • Commands: CLI commands for different operations (fix, list, etc.)
  • Rules: Individual deprecation fix implementations
  • Processors: File processing and transformation logic
  • Utils: Shared utilities for file operations, logging, etc.

Contributing 🀝 #

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

Adding a new deprecation fix: #

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/new-deprecation-fix)
  3. Add your deprecation rule with tests
  4. Ensure all tests pass and code follows Very Good Analysis standards
  5. Commit your changes (git commit -m 'Add new deprecation fix for XYZ')
  6. Push to the branch (git push origin feature/new-deprecation-fix)
  7. Open a Pull Request

Built with Claude Code by Moinsen Development Β© 2025
Bootstrapped with πŸ’™ by Very Good CLI

1
likes
160
points
16
downloads

Publisher

unverified uploader

Weekly Downloads

A powerful and extensible Dart CLI tool that automatically fixes Flutter deprecations in your codebase

Repository (GitHub)
View/report issues

Documentation

Documentation
API reference

License

MIT (license)

Dependencies

args, cli_completion, equatable, mason_logger, meta, path, pub_updater

More

Packages that depend on fix_flutter_deprecations