use_ string_ buffers
Use a string buffer rather than '+' to compose strings.
Description
#
The analyzer produces this diagnostic when values are concatenated to a
string inside a loop without using a
StringBuffer
to do the
concatenation.
Example
#
The following code produces this diagnostic because the string
result
is
computed by repeated concatenation within the
for
loop:
String f() {
var result = '';
for (int i = 0; i < 10; i++) {
result += 'a';
}
return result;
}
Common fixes
#Use a StringBuffer
to compute the result:
String f() {
var buffer = StringBuffer();
for (int i = 0; i < 10; i++) {
buffer.write('a');
}
return buffer.toString();
}
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Page last updated on 2025-9-1. View source or report an issue.