【发布时间】:2021-06-01 01:50:57
【问题描述】:
我有一个简单的地图“对象”让我们假设:
{
ServoP180V1: {X: 100.0, Y: 0.0},
ServoP180V3: {X: 100.0, Y: 0.0}
ServoP180V5: {X: 100.0, Y: 0.0}
}
我如何对键进行排序,使其按顺序排列,如下所示:
{
ServoP180V1: {X: 100.0, Y: 0.0},
ServoP180V2: {X: 100.0, Y: 0.0}
ServoP180V3: {X: 100.0, Y: 0.0}
}
我试过这段代码,但有时返回 null 有问题,不确定我的方法是否正确
sortObjects() {
int i = 1;
for (var key in objects.keys) {
objects.update(
key.substring(0, key.length - 1) + i.toString(),
null,
);
i++;
}
}
The method 'call' was called on null.
Receiver: null
Tried calling: call()
也是这样
sortObjects() {
int i = 1;
objects.forEach((key, value) {
objects.update(
key.substring(0, key.length - 1) + i.toString(),
(existingValue) => value,
ifAbsent: () => value,
);
i++;
});
}
给出这样的错误
Exception: Concurrent modification during iteration:
提前谢谢你!
【问题讨论】:
-
您似乎正在将键从旧结构更改为新结构。什么是.update?
标签: algorithm flutter dart async-await hashmap