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