flutter_custom_lints 1.0.0
flutter_custom_lints: ^1.0.0 copied to clipboard
A collection of custom lint rules for Flutter and Dart projects that enforce best practices and prevent common coding mistakes.
Flutter Custom Lints #
A collection of custom lint rules for Flutter and Dart projects that enforce best practices and prevent common coding mistakes.
Installation #
Add flutter_custom_lints to your dev_dependencies in pubspec.yaml:
dev_dependencies:
custom_lint: ^0.7.5
flutter_custom_lints: ^1.0.0
Then run:
dart pub get
Setup #
Add the following to your analysis_options.yaml:
analyzer:
plugins:
- custom_lint
custom_lint:
rules:
# Enable specific rules
- no_as_type_assertion
- no_direct_iterable_access
- no_null_force
- use_compare_without_case
Available Lints #
no_as_type_assertion #
Prevents using as for type assertions which can cause runtime errors.
❌ Bad:
var user = data as User; // Can throw at runtime
✅ Good:
if (data is User) {
var user = data; // Safe type promotion
}
no_direct_iterable_access #
Prevents direct access to iterables without null checks.
❌ Bad:
var first = list.first; // Can throw if list is empty
✅ Good:
var first = list.isNotEmpty ? list.first : null;
// or create your own safe extension:
extension SafeIterable<T> on Iterable<T> {
T? get safeFirst => isEmpty ? null : first;
T? safeAt(int index) => index < 0 || index >= length ? null : elementAt(index);
}
no_null_force #
Prevents force unwrapping of nullable values.
❌ Bad:
String value = nullableString!; // Can throw at runtime
✅ Good:
String? value = nullableString;
if (value != null) {
// Use value safely
}
use_compare_without_case #
Suggests using case-insensitive string comparison methods.
❌ Bad:
if (str1.toLowerCase() == str2.toLowerCase()) {
// Less efficient
}
✅ Good:
// Create your own extension method:
extension StringComparisonExtension on String {
bool compareWithoutCase(String other) =>
toLowerCase() == other.toLowerCase();
}
// Then use it:
if (str1.compareWithoutCase(str2)) {
// More efficient and readable
}
Running Lints #
After setup, you can run the lints using:
dart run custom_lint
For Flutter projects:
flutter packages pub run custom_lint
Contributing #
Contributions are welcome! Please feel free to submit a Pull Request.
License #
This project is licensed under the MIT License - see the LICENSE file for details.
Development Setup #
This project uses FVM (Flutter Version Management) to ensure consistent Flutter SDK versions across different development environments.
Prerequisites #
-
Install FVM globally:
dart pub global activate fvm -
Install the project's Flutter version:
fvm install
Development Commands #
You can use FVM commands directly:
# Install dependencies
fvm dart pub get
# Run tests
fvm dart test
# Run static analysis
fvm dart analyze
# Format code
fvm dart format .
Debugging #
The project includes VS Code launch configurations for debugging:
- Debug Tests (FVM) - Debug all tests
- Debug Single Test File (FVM) - Debug the currently open test file
VS Code Configuration #
The project is configured to:
- Use FVM's Flutter SDK automatically
- Disable automatic pub get (use manual commands instead)
- Format code on save
- Apply code fixes on save