Skip to main content

prefer-bytes-builder

Warns when an expensive byte list operation is used.

Using lists concatenation, addAll or spread operator can significantly affect the performance. It's recommended to use a BytesBuilder instead.

Additional resources:

info

The rule by default will trigger on every list of integers. It's up to you to scope it to files where you work with bytes.

Example

❌ Bad:

void run() {
List<int> buffer = Uint8List(0);
for (var i = 0; i < 100; i++) {
final Uint8List chunk = chunks[i];
final List<int> intChunk = chunks[i];

buffer = buffer + chunk; // LINT
buffer = buffer + intChunk; // LINT
buffer += chunk; // LINT
buffer += intChunk; // LINT
}
buffer;
}

✅ Good:

void run() {
final buffer = BytesBuilder();
for (var i = 0; i < 100; i++) {
buffer.add(chunks[i]);
}
buffer.toBytes();
}