avoid-missing-interpolation
Warns when a string is equal to a variable name that is available in the current scope but is not wrapped into an interpolation.
Missing interpolations can lead to unexpected results when the variable name is used instead of its value (e.g. 'name' instead of the value of the name
variable).
⚙️ Config
Set ignore-non-string-types
(default is true
) to ignore variables with non-string type (example).
analysis_options.yaml
dart_code_metrics:
rules:
- avoid-missing-interpolation:
ignore-non-string-types: true
Example
❌ Bad:
void main() {
final someClass = SomeClass('hello');
print('someClass');
print('value: $someClass.value'); // LINT: This interpolation is potentially incomplete and should be wrapped into {}
print('isNotEmpty: $someClass.value.isNotEmpty'); // LINT: This interpolation is potentially incomplete and should be wrapped into {}
print('$function(value)'); // LINT: This interpolation is potentially incomplete and should be wrapped into {}
}
String function(String value) {
print('value'); // LINT: This string contains a variable name. Ensure it should not have an interpolation.
return 'result value';
}
class SomeClass {
final String value;
const SomeClass(this.value);
}
✅ Good:
void main() {
final someClass = SomeClass('hello');
print('someClass');
print('value: ${someClass.value}'); // Correct, wrapped in interpolation
print('isNotEmpty: ${someClass.value.isNotEmpty}'); // Correct, wrapped in interpolation
print('${function(value)}'); // Correct, wrapped in interpolation
}
String function(String value) {
print(value); // Correct, uses the variable
return 'result value';
}
Example with "ignore-non-string-types"
Config
analysis_options.yaml
dart_code_metrics:
rules:
- avoid-missing-interpolation:
ignore-non-string-types: false
❌ Bad:
void main() {
final someClass = SomeClass('hello');
print('someClass'); // LINT: This string contains a variable name. Ensure it should not have an interpolation.
}
String function(String value) {
print('value'); // LINT: This string contains a variable name. Ensure it should not have an interpolation.
return 'result value';
}
✅ Good:
void main() {
final someClass = SomeClass('hello');
print(someClass); // Correct, uses the variable
}