Flutter Pre-Commit
A unified pre-commit hook tool for Flutter projects that automatically runs code formatting and static analysis checks before committing code.
Features
- Automatically installs Git pre-commit hook
- Runs
dart formatto check code formatting before commit - Runs
flutter analyzefor static code analysis before commit - Only checks Dart files in the staging area
- Provides friendly error messages
- Easy to install and configure
Installation
Add Dependency
Add flutter_pre_commit to the dev_dependencies section of your pubspec.yaml file:
dev_dependencies:
flutter_pre_commit: ^0.2.4
Then run:
flutter pub get
Install Pre-commit Hook
Run the following command to install the pre-commit hook:
flutter pub run flutter_pre_commit
This will install the pre-commit hook in your Git repository and create necessary configuration files.
Usage
Once installed, the pre-commit hook will run automatically. When you try to commit code, the hook will:
- Check Dart files in the staging area
- Run
dart formatto check code formatting - Run
flutter analyzefor static analysis - Block the commit and display error messages if checks fail
Example Output
Success:
🚀 Running Flutter pre-commit checks...
✅ All checks passed!
Failure:
🚀 Running Flutter pre-commit checks...
❌ Dart format check failed. Please run 'dart format .' to fix formatting issues.
[Error details]
Configuration
Analysis Options
flutter_pre_commit will create or update the analysis_options.yaml file in your project root. The default configuration includes:
# Include flutter_pre_commit default rules
include: package:flutter_lints/flutter.yaml
# You can add or override rules here
linter:
rules:
prefer_const_constructors: true
depend_on_referenced_packages: false
# Custom rules
You can modify this file to customize analysis rules.
Force Update
To force update an existing pre-commit hook, use the --force option:
flutter pub run flutter_pre_commit --force
Troubleshooting
Hook Not Running
Ensure the Git hook has execute permissions:
chmod +x .git/hooks/pre-commit
Skipping the Hook
In special cases, if you need to skip pre-commit checks, you can use the --no-verify option:
git commit -m "Your commit message" --no-verify
However, this is not recommended as it bypasses code quality checks.
Contributing
Contributions via Pull Requests or Issues are welcome.