https://github.com/polemius/avoid_non_null_assertion_operator
https://github.com/polemius/avoid_non_null_assertion_operator
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/polemius/avoid_non_null_assertion_operator
- Owner: polemius
- License: mit
- Created: 2025-03-27T21:05:45.000Z (2 months ago)
- Default Branch: main
- Last Pushed: 2025-04-04T05:56:49.000Z (2 months ago)
- Last Synced: 2025-04-04T06:30:02.133Z (2 months ago)
- Language: Dart
- Size: 20.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# avoid_non_null_assertion_operator
A custom lint package for Dart/Flutter that helps enforce safer null handling by prohibiting the use of the non-null assertion operator (!).
## Features
- Detects and reports usage of the non-null assertion operator (!)
- Helps enforce safer null handling practices
- Integrates with Dart's analyzer## Getting started
Run:
```shell
dart pub add --dev custom_lint avoid_non_null_assertion_operator
```Or if using Flutter:
```shell
flutter pub add --dev custom_lint avoid_non_null_assertion_operator
```Add this to your package's `pubspec.yaml` file:
```yaml
dev_dependencies:
custom_lint:
avoid_non_null_assertion_operator:
```## Usage
Add the following to your `analysis_options.yaml`:
```yaml
analyzer:
plugins:
- custom_lint
errors:
avoid_non_null_assertion_operator: error
```The lint will now report errors for code like this:
```dart
String? nullable = "test";
String nonNullable = nullable!; // Error: Avoid using the non-null assertion operator (!)
```Instead, use safer alternatives like:
```dart
String? nullable = "test";
String nonNullable = nullable ?? "default"; // Use null coalescing
// or
if (nullable != null) {
String nonNullable = nullable; // Use null check
}
```## Additional information
This package is designed to help maintain safer null handling practices in Dart/Flutter projects. It encourages the use of explicit null checks and null coalescing operators instead of force-unwrapping nullable values.
For more information about null safety in Dart, visit the [official documentation](https://dart.dev/null-safety).