I have a list that I am getting the values from an API,
it is List<dynamic>
type, while I print
it I am getting this output (for example): [cat, female]
, but when I use inspect
it has three values: "cat", "female", ""
. The last empty value is making some problems in my code, so I wanted to remove it, but I don't know how to do this.
As it is a List<dynamic>
I used removeLast()
and also toString()
but none of them worked for me. I appreciate any help on this.
CodePudding user response:
The solution is to filter items and get the new list:
final newList = list.where((e) => e != null && e != '').toList();
removeLast()
does not work as you expected. If you read the comment of the method it says Removes and returns the last object in this list.
PS: I recommend you use a functional way to deal with a list which means do not modify the state of the original list instead, get a new list.