68 lines
1.7 KiB
Plaintext
68 lines
1.7 KiB
Plaintext
String interpolation should be used only when there is a mix of variables and string in the same expression.
|
|
|
|
== Why is this an issue?
|
|
|
|
String interpolation is a powerful feature in Dart that allows you to embed expressions within string literals.
|
|
|
|
[source,dart]
|
|
----
|
|
final expression = 'expressions';
|
|
print('a string mixing ${"literals"} and ${2 + 1} ${expressions}');
|
|
// Prints 'a string mixing literals and 3 expressions'
|
|
----
|
|
|
|
However, using string interpolation where it is not needed can make the code harder to read and maintain.
|
|
|
|
[source,dart]
|
|
----
|
|
final aString = 'a string';
|
|
print('$aString'); // Unnecessary string interpolation
|
|
----
|
|
|
|
It is recommended to use string interpolation only when there is a mix of variables and strings in the same expression.
|
|
|
|
== How to fix it
|
|
|
|
Remove the string interpolation and use the interpolated string directly.
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,dart,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
final aString = 'a string';
|
|
print('$aString'); // Non compliant
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,dart,diff-id=1,diff-type=compliant]
|
|
----
|
|
final aString = 'a string';
|
|
print(aString);
|
|
----
|
|
|
|
== Resources
|
|
|
|
=== Documentation
|
|
|
|
* Dart Docs - https://dart.dev/tools/linter-rules/unnecessary_string_interpolations[Linter rule - unnecessary_string_interpolations]
|
|
* Dart Docs - https://dart.dev/language/built-in-types#strings[Language - Strings]
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Unnecessary use of string interpolation.
|
|
|
|
=== Highlighting
|
|
|
|
The entire string, including the string delimiters (`'`, `"`, `'''` or `"""`).
|
|
|
|
endif::env-github,rspecator-view[]
|