dart
Remove empty lists from a list of lists and flatten it using Dart
If you have a list created using Dart and it contains multiple lists inside it. Some of the lists contain no items and you want to get rid of those empty lists along with flattening the list. We are explaining the methods in Dart to do it.
void main() {
List listOfLists = [['a', 'b', 'c'], [], ['d', 'e'], [], [], ['f', 'g', 'h'], [], []];
List result = listOfLists.expand((x) => x).toList();
print(result);
}
Output
[a, b, c, d, e, f, g, h]
A Dart List is a collection of elements that can be used to store and access data using the index. The List is a mutable collection means you can use add or remove items from it.
We are using here List.expand() function to flatten a list of lists and remove empty lists for it.
Was this helpful?
Similar Posts
- Remove empty and falsey values from a List in Dart/Flutter
- Check if list is empty in Flutter/Dart
- Remove List item or element using its index in Dart
- Check if a String is empty or null in Dart / Flutter
- Dart Program to remove the last element from a List
- Dart Program to remove multiple items from a List at once
- Dart program to remove duplicate items from a list