avoid-nullable-interpolation
preset: recommended
Warns when an interpolation string contains a nullable value.
Using nullable values in interpolations can lead to unexpected output. Consider checking the value to be non-null before using it in the interpolation.
⚙️ Config
Set ignored-invocations
(default is empty) to ignore interpolation strings within specific invocations (example).
analysis_options.yaml
dart_code_metrics:
rules:
- avoid-nullable-interpolation:
ignored-invocations:
- log
- debugPrint
Example
❌ Bad:
void function(String? value) {
print('$value hello'); // LINT: This interpolation is potentially nullable. Try checking for a null value first.
}
✅ Good:
void function(String? value) {
if (value != null) {
print('$value hello'); // Correct, checked for not null first
}
print('${value != null ? value : ''} hello');
}
Example with "ignored-invocations"
Config
analysis_options.yaml
dart_code_metrics:
rules:
- avoid-nullable-interpolation:
ignored-invocations:
- debugPrint
void function(String? value) {
debugPrint('output: $value'); // Correct, 'debugPrint' is ignored
}