partition method
partition
Splits a List
into the separate list chunks depending on the
specified chunkSize
. Chunk size will default to 2
Usage:
List<String> words = ["Hello", "World", "Name", "Is", "Ngoni"]
List<List<String>> partitionedWords = words.partition(chunkSize: 2)
Result:
[["Hello", "World"], ["Name", "Is"], ["Ngoni"]]
Implementation
List<List<E>> partition({int chunkSize = 2}) {
final List<List<E>> chunks = [];
for (int i = 0; i < length; i += chunkSize) {
chunks.add(
sublist(i, (i + chunkSize) > length ? length : (i + chunkSize))
.toList(),
);
}
return chunks;
}