Home > Software engineering >  IN DART - toList() method is not converting an Iterable of int into list, but when it is applied ins
IN DART - toList() method is not converting an Iterable of int into list, but when it is applied ins

Time:06-14

I have created a list 'numbers' then used 'map' method to loop over it and assigned the output to squares. Now I apply toList() method on 'squares' to convert it to a List type but in output it's printed as iterable of int. However when I do the same thing inside the print() method then it gives me the output as a list. So why is it not working outside the print() method?

 void main() {
  const numbers = [1, 2, 3, 4];
  final squares = numbers.map((number) => number * number);
  squares.toList(); // I've applied toList() method here

  print(squares); // But I don't get a list from this output
  print(squares.toList()); // But I get it from this output. Why?

}

OUTPUT (1, 4, 9, 16) [1, 4, 9, 16]

CodePudding user response:

toList() convert the data to list but you have to assign the result to a variable like this:

List list = squares.toList();

Then you can use the new variable.

  • Related