Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pedrolemoz/693ba58dd6124c61f6b7e3190601b09e to your computer and use it in GitHub Desktop.
Save pedrolemoz/693ba58dd6124c61f6b7e3190601b09e to your computer and use it in GitHub Desktop.
Split list into lists of values with type Generic
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