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.
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: Prefer 'BytesBuilder' to compose bytes. Try rewriting the code to use it instead.
buffer = buffer + intChunk; // LINT: Prefer 'BytesBuilder' to compose bytes. Try rewriting the code to use it instead.
buffer += chunk; // LINT: Prefer 'BytesBuilder' to compose bytes. Try rewriting the code to use it instead.
buffer += intChunk; // LINT: Prefer 'BytesBuilder' to compose bytes. Try rewriting the code to use it instead.
}
}
✅ Good:
void run() {
final buffer = BytesBuilder();
for (var i = 0; i < 100; i++) {
buffer.add(chunks[i]);
}
buffer.toBytes();
}