在這個端點http://www.caguascriollos.com/nuevaweb/wp-json/sportspress/v2/tables我得到了一個地圖串列。在每個元素中,在“資料”鍵中,我有另一個串列。該串列有一個“pos”(位置)鍵。
我想根據“pos”值對每個“資料”進行排序,但出現以下錯誤:
NoSuchMethodError (NoSuchMethodError: Class '_InternalLinkedHashMap<String, dynamic>' has no instance method 'sort'.
Receiver: _LinkedHashMap len:6
Tried calling: sort(LinkedHashSet len:1))
我的代碼:
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())
}
}
});
}
uj5u.com熱心網友回復:
首先獲取data元素并將其隔離。您可以將您的data地圖條目轉換為一個串列,然后進行比較pos,例如:
// 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);
這只是一個原型。您需要確保沒有 nullpos并且資料型別是預期的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/372199.html
