-
-
Save MillerAdulu/9a38e0afcd260f2107106f42f5683f2a to your computer and use it in GitHub Desktop.
Extension to chunk list
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() => 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