https://github.com/moinsen-dev/fix_flutter_deprecations
A powerful and extensible Dart command-line tool that automatically fixes Flutter deprecations in your codebase.
https://github.com/moinsen-dev/fix_flutter_deprecations
Last synced: 7 months ago
JSON representation
A powerful and extensible Dart command-line tool that automatically fixes Flutter deprecations in your codebase.
- Host: GitHub
- URL: https://github.com/moinsen-dev/fix_flutter_deprecations
- Owner: moinsen-dev
- License: mit
- Created: 2025-08-02T06:59:17.000Z (9 months ago)
- Default Branch: develop
- Last Pushed: 2025-08-02T10:24:02.000Z (9 months ago)
- Last Synced: 2025-08-02T10:31:43.505Z (9 months ago)
- Language: Dart
- Size: 3.04 MB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# 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 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
```sh
dart pub global activate fix_flutter_deprecations
```
### Local Development
```sh
dart pub global activate --source=path .
```
## Usage ๐
### Fix all deprecations in your project
```sh
fix_deprecations
```
### Preview changes without applying them (dry run)
```sh
fix_deprecations --dry-run
```
### Apply specific deprecation fixes
```sh
fix_deprecations --rules withOpacity,willPopScope,buildContextAsync
```
### Fix a specific file or directory
```sh
fix_deprecations lib/src/widgets/
fix_deprecations lib/main.dart
```
### Create backups before fixing
```sh
fix_deprecations --backup
```
### List all available deprecation rules
```sh
fix_deprecations --list-rules
```
## Command Reference
```sh
# 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:**
```dart
WillPopScope(
onWillPop: () async {
return await showDialog(...);
},
child: Scaffold(...),
)
```
**After:**
```dart
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:**
```dart
Future _handleSubmit() async {
await _submitForm();
Navigator.of(context).pop(); // โ ๏ธ use_build_context_synchronously
}
```
**After:**
```dart
Future _handleSubmit() async {
await _submitForm();
if (mounted) {
Navigator.of(context).pop(); // โ
Safe to use
}
}
```
### Multiple Underscores Fix
**Before:**
```dart
class MyClass {
String __privateField; // โ ๏ธ unnecessary multiple underscores
}
```
**After:**
```dart
class MyClass {
String _privateField; // โ
Single underscore
}
```
## Running Tests with coverage ๐งช
To run all unit tests use the following command:
```sh
$ 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](https://github.com/linux-test-project/lcov)
.
```sh
# 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:
```dart
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](https://claude.ai/code) by [Moinsen Development](https://moinsen.dev) ยฉ 2025
Bootstrapped with ๐ by [Very Good CLI][very_good_cli_link]
[very_good_cli_link]: https://github.com/VeryGoodOpenSource/very_good_cli