Created
May 13, 2021 14:14
-
-
Save ahmtcn123/764057ca4c15d94a5c39c0b1f20046fc to your computer and use it in GitHub Desktop.
A list chunker written in dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Author
ahmtcn123
commented
May 13, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment