Created
July 2, 2021 15:45
-
-
Save pedrolemoz/693ba58dd6124c61f6b7e3190601b09e to your computer and use it in GitHub Desktop.
Split list into lists of values with type Generic
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() { | |
final data = List.generate(10, (index) => index); | |
final result = splitListIntoListsOfValuesWithType<int>(splitQuantity: 4, elements: data); | |
print(result); | |
} | |
List<List<T>> splitListIntoListsOfValuesWithType<T>({List<T> elements, int splitQuantity}) { | |
List<List<T>> resultList = []; | |
for (var index = 0; index < elements.length; index += splitQuantity) { | |
resultList.add( | |
elements.sublist( | |
index, | |
index + splitQuantity > elements.length ? elements.length : index + splitQuantity, | |
), | |
); | |
} | |
return resultList; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment