2

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 Answer 1

2
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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.