-1

I have an issue in my Dart Programme. Here is a list of emojis:

[[, , ], [], [, , ], [], [], []] //List

Inside the list there are multiple lists (as you can see). I want to remove empty lists []. Also I want to Join [, , , , , ] into a single list.

Pro Co
  • 341
  • 3
  • 10

2 Answers2

3

Full Solution:

var data = [['', '', ''], [], ['', '', ''], [], [], []];

void main() {
  var newData = data.expand((x) => x).toList();
  print(newData);
}

Here, newData is your final answer as: [, , , , , ].

Yatinj Sutariya
  • 506
  • 5
  • 15
  • This Helped a Lot, But can You Explain me Yatinj Why "x" is used in this List – Pro Co Apr 16 '21 at 10:05
  • Here, `x` refers to an individual element of your `data` list. You can see [here](https://api.dart.dev/stable/1.10.1/dart-core/List/expand.html). – Yatinj Sutariya Apr 16 '21 at 10:12
  • Like, "i" we have in loops? – Pro Co Apr 16 '21 at 10:24
  • You can not say `i`, because `i` refers to an index basically in the loop. Here `x` shows that individual element. For your given case, in the first iteration, the value of `x` will be `['', '', '']`, in the second iteration, the value of `x` will be `[]`, for third `['', '', '']` and so on. – Yatinj Sutariya Apr 16 '21 at 11:10
1

You can do this in one line:

yourData.expand((x) => x).toList();
Mäddin
  • 1,070
  • 6
  • 11