Skip to content

Instantly share code, notes, and snippets.

@MillerAdulu
Forked from jorwan/chunk_list_sample.dart
Created October 16, 2021 13:39
Show Gist options
  • Save MillerAdulu/9a38e0afcd260f2107106f42f5683f2a to your computer and use it in GitHub Desktop.
Save MillerAdulu/9a38e0afcd260f2107106f42f5683f2a to your computer and use it in GitHub Desktop.
Extension to chunk list
void main() => print(List.generate(10, (i) => i).chunk(3));
extension ListChunkerExtension on List {
List chunk(int chunkLength) {
if ((chunkLength ?? 0) <= 0 || (this ?? []).length == 0)
return this;
var chunks = [];
for (var i = 0; i < this.length; i += chunkLength) {
chunks.add(this.sublist(
i, i + chunkLength > this.length ? this.length : i + chunkLength));
}
return chunks;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment