Issue
In this endpoint http://www.caguascriollos.com/nuevaweb/wp-json/sportspress/v2/tables I'm getting a List of Maps. Inside each element, in the "data" key, I have another List. That List have a "pos" (position) key.
I want to sort each "data" depending on the "pos" value, but I'm getting the following error:
NoSuchMethodError (NoSuchMethodError: Class '_InternalLinkedHashMap<String, dynamic>' has no instance method 'sort'.
Receiver: _LinkedHashMap len:6
Tried calling: sort(LinkedHashSet len:1))
My code:
final response = await http.get(Uri.parse('http://www.caguascriollos.com/nuevaweb/wp-json/sportspress/v2/tables'));
_positions = json.decode(response.body);
for (var _position in _positions!) {
_position['data'].sort({
(a,b) => {
if (a.isInt() && b.isInt()) {
a.toInt().compareTo(b.toInt())
}
}
});
}
Solution
Firs obtain the data
element and isolate it.
You can convert your data
map entries to a list and then compare by pos
, for example:
// Data looks like this:
// final data = {"0": {"pos": 1}, "5394": {"pos": 2}, "5395": {"pos": 0}};
// Convert dictionary entries to list
final l = data.entries.toList();
// Sort by the value of pos
l.sort((e1, e2) => Comparable.compare(e1.value["pos"]!, e2.value["pos"]!));
// l is sorted by pos
print(l);
This is just a prototype. You need to make sure there are no null pos
s and that data types are the expected.
Answered By - Eddie Lopez
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.