Skip to content

Instantly share code, notes, and snippets.

@ahmtcn123
Created May 13, 2021 14:14
Show Gist options
  • Save ahmtcn123/764057ca4c15d94a5c39c0b1f20046fc to your computer and use it in GitHub Desktop.
Save ahmtcn123/764057ca4c15d94a5c39c0b1f20046fc to your computer and use it in GitHub Desktop.
A list chunker written in dart
class Chunker<T> {
Chunker(this.items, this.chunkCount);
final int chunkCount;
final List<T> items;
List<List<T>> chunks() {
List<List<T>> chunks = [];
List<T> tempList = [];
int count = 0;
for (int i = 0; i <= this.items.length - 1; i++) {
if (count == this.chunkCount) {
chunks.add(tempList);
tempList = [];
count = 0;
} else {
tempList.add(this.items[i]);
count++;
}
}
if (tempList.length != 0) {
chunks.add(tempList);
}
return chunks;
}
}
@ahmtcn123
Copy link
Author

var categories = [
     1,
     2,
     3,
     4,
     5,
     6,
     7,
     8,
     9
];
var chunks = Chunker(categories, 3).chunks();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment