1

I've got a MappedListIterable than I know to sort

When calling the sort method , I 'm getting

EXCEPTION: NoSuchMethodError: Class 'MappedListIterable' has no instance method 'sort'. Receiver: Instance of 'MappedListIterable' Tried calling: sort(Closure: (dynamic, dynamic) => dynamic)

Peter Badida
  • 10,502
  • 9
  • 40
  • 84
bwnyasse
  • 481
  • 1
  • 7
  • 15

1 Answers1

10

You get a MappedListIterable after calling .map(f) on an Iterable.

The Iterable class does not have a sort() method. This method is on List.

So you first need to get a List from your MappedListIterable by calling .toList() for example.

var i = [1, 3, 2].map((i) => i + 1);
// i is a MappedListIterable
// you can not call i.sort(...)

var l = i.toList();
l.sort(); // works

Or in one line (code-golf):

var i = [1, 3, 2].map((i) => i + 1).toList()..sort();
Szczerski
  • 539
  • 7
  • 7
Alexandre Ardhuin
  • 62,400
  • 12
  • 142
  • 126
  • in one line ..sort works but why double dots? why not .sort? can you help me understand? or, please refer any documentation link. – Shunjid Rahman Feb 10 '20 at 15:55
  • 1
    See https://stackoverflow.com/questions/49447736/list-use-of-double-dot-in-dart/49447872#49447872 . `.sort()` returning `void` (sorting is done inplace) you can not assign the result to a variable. – Alexandre Ardhuin Feb 10 '20 at 16:24