vicky_prog
vicky_prog

Reputation: 15

How do I spilt list with dart?

Assume I have a list like:

var numbers = ["1","2","3","4","5"];

I would like a list spilt list:

var numbers = ["1","1", "2","3", "1","2","3", "1","2","3","4", "1","2","3","4","5"];

what's a good way to do this with dart?

Upvotes: 2

Views: 76

Answers (1)

nvoigt
nvoigt

Reputation: 77285

It's a little tricky to know what you want, because your expected output does not actually line up with what you seem to want. I will assume psink was correct in guessing and you want that:

void main(List<String> args) {
  final input = ["A","B","C","D","E"]; 
  
  final output = List.generate(input.length, (i) => input.sublist(0, i + 1)).expand((i) => i).toList();
  
  print(input.runtimeType);
  print(input);
  
  print(output.runtimeType);
  print(output);
}

That in dartpad generates

JSArray

[A, B, C, D, E]

JSArray

[A, A, B, A, B, C, A, B, C, D, A, B, C, D, E]

Which hopefully is what you need. Thanks to psink for the actual code.

Upvotes: 1

Related Questions