fix_flutter_deprecations 0.1.2
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 #
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.
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 deprecatedWillPopScopetoPopScopewith intelligent callback transformation- Simple boolean returns β
canPopproperty - Complex logic β
onPopInvokedcallbacks with proper navigation handling
- Simple boolean returns β
π§ Linting Rule Fixes
-
multipleUnderscores: Fixes "unnecessary use of multiple underscores" warnings- Preserves generated code patterns and test mocks
- Converts
__identifierto_identifierwhere appropriate
-
buildContextAsync: Fixesuse_build_context_synchronouslywarnings- 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:
- Create a new rule class in
lib/src/rules/ - Implement the
DeprecationRuleinterface - 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: #
- Fork the repository
- Create your feature branch (
git checkout -b feature/new-deprecation-fix) - Add your deprecation rule with tests
- Ensure all tests pass and code follows Very Good Analysis standards
- Commit your changes (
git commit -m 'Add new deprecation fix for XYZ') - Push to the branch (
git push origin feature/new-deprecation-fix) - Open a Pull Request
Built with Claude Code by Moinsen Development Β© 2025
Bootstrapped with π by Very Good CLI