I'm getting a type error while using list.map function

Viewed 234

I'm getting a type error when running the following code in DartPad.

void main() {
  List<int> list = [10, 20, 30];
  List<int> squares = list.map((x) => x * x);
  squares.forEach((x) => print(x));
}

Getting the following error

Uncaught exception:
TypeError: Instance of 'MappedListIterable<int, int>': 
type 'MappedListIterable<int, int>' is not a subtype of 
type 'List<int>'

What could be wrong here?

1 Answers
List<int> squares = list.map((x) => x * x);

would need to be

List<int> squares = list.map((x) => x * x).toList();

map() returns an Iterable, not a List

Related