Created
July 16, 2021 00:03
-
-
Save riscait/f4c95ade1546c9c040566212532aa8ad to your computer and use it in GitHub Desktop.
Chunk list by sublist
This file contains hidden or 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
void main() { | |
final items = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']; | |
final chunkedItems = []; | |
final chunkSize = 3; | |
for (var i = 0; i < items.length; i += chunkSize) { | |
final isLast = i + chunkSize >= items.length; | |
final end = isLast ? items.length : i + chunkSize; | |
chunkedItems.add(items.sublist(i, end)); | |
} | |
print(chunkedItems); // [[a, b, c], [d, e, f], [g, h, i], [j, k]] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment