I have to make List<List>
using List
List<String> list = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10' , '11', '12', '13, '14', '15, '16, '17, '18' , '19', , '20', '21', '22', '23', '24', '25'];
list.length
will be no more than 25.
have to divide by 5 like
int divide;
divide = word.length ~/ 5;
and have to make List<List>
I'm trying to make it but my head is not working well any one can give idea???
have to be
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20],[21, 22, 23, 24, 25]]
if list.length is 23
have to be
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20],[21, 22, 23]]
CodePudding user response:
You can try this one
List dataList = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10' , '11', '12', '13', '14', '15', '16', '17', '18' , '19', '20', '21', '22'];
List chunkList = [];
int chunkSize = 5;
for (var i = 0; i < dataList.length; i = chunkSize) {
chunkList.add(dataList.sublist(i, i chunkSize > dataList.length ? dataList.length : i chunkSize));
}
print(chunkList);
CodePudding user response:
I think this is the best one, you can try like below
List<List<String>> _getListInList(List<String> data) {
final chunks = <List<String>>[];
final chunkSize = 5;
for (var i = 0; i < data.length; i = chunkSize) {
chunks.add(
data.sublist(
i,
i chunkSize > data.length ? data.length : i chunkSize,
),
);
}
return chunks;
}
Just Copy and Paste :D
CodePudding user response:
public class MyList<T>
{
public List<T> List {get;set;}
}