3

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
  • 11,310
  • 10
  • 44
  • 90
bwnyasse
  • 591
  • 1
  • 10
  • 15

1 Answers1

13

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
  • 839
  • 11
  • 11
Alexandre Ardhuin
  • 71,959
  • 15
  • 151
  • 132
  • 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
  • When I do a toList() on my MappedListIterable, I get an error: Error: Expected a value of type 'Map', but got one of type 'List' – Steve3p0 Jul 08 '23 at 05:20