Skip to content

Instantly share code, notes, and snippets.

@jorwan
Last active May 28, 2023 15:43
Show Gist options
  • Save jorwan/968ebf2e3e957c2d074efdf660efa200 to your computer and use it in GitHub Desktop.
Save jorwan/968ebf2e3e957c2d074efdf660efa200 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