【发布时间】:2022-12-12 00:27:53
【问题描述】:
我有一个函数可以过滤来自 api 的数据并将过滤后的数据设置为新地图:
_addUpAndGetPercentage(List states) {
Map<String, dynamic> _states = {
"Stress": [],
"Fatigue": [],
"Anxiety": [],
"Relaxation": [],
"Involvement": []
};
log("states === $states"); // states === [{externalSessionId: 19dd1148-97e3-4e33-8cd6-9d4ab3b8d64c, iaf: 11, iapf: 9, startTime: null, endTime: null, states: {Anxiety: 2, Stress: 1, Chronic Fatigue: 1}}, {externalSessionId: d1d6637a-a445-467d-b658-91e42b080ed6, iaf: 12, iapf: 8, startTime: null, endTime: null, states: {Stress: 1, Involvement: 1, Slight Fatigue: 2, Anxiety: 5, Relaxation: 2, Chronic Fatigue: 3}}]
for (int i = 0; i < states.length; i++) {
var shortcut = states[i]["states"];
log("Stress ==== ${states[i]['states'].toString()}");
if (states[i]["Stress"] != null) {
_states[i]["Stress"].add(states[i]["Stress"]);
} else {
log("ERROOROROROOROR ==== ${states[i]['states'].toString()}");
_states["Stress"].add(0);
}
if (shortcut["Anxiety"] != null) {
_states["Anxiety"].add(shortcut["Anxiety"]);
} else {
_states["Anxiety"].add(0);
}
}
log("_states ===== ${_states.toString()}");
return _states;
}
当我更改时出现错误状态[i][“压力”]至捷径[“压力”]=>NoSuchMethodError:在 null 上调用了方法“[]”。
但是 log("Stress ==== ${states[i]['states'].toString()}"); 返回我 Stress ==== {Anxiety: 2, Stress: 1, Chronic Fatigue: 1}
捷径[“焦虑”]工作正常,不会给我返回一个错误,只有捷径[“压力”]我不明白为什么..
如果我删除捷径[“压力”]并设置状态[i][“压力”]然后没有出现错误,但它没有像我想要的那样工作,这是我设置时的日志状态[i][“压力”]:
states === [{externalSessionId: 19dd1148-97e3-4e33-8cd6-9d4ab3b8d64c, iaf: 11, iapf: 9, startTime: null, endTime: null, states: {Anxiety: 2, Stress: 1, Chronic Fatigue: 1}}, {externalSessionId: d1d6637a-a445-467d-b658-91e42b080ed6, iaf: 12, iapf: 8, startTime: null, endTime: null, states: {Stress: 1, Involvement: 1, Slight Fatigue: 2, Anxiety: 5, Relaxation: 2, Chronic Fatigue: 3}}]
[log] Stress ==== {Anxiety: 2, Stress: 1, Chronic Fatigue: 1}
[log] ERROOROROROOROR ==== {Anxiety: 2, Stress: 1, Chronic Fatigue: 1}
[log] Stress ==== {Stress: 1, Involvement: 1, Slight Fatigue: 2, Anxiety: 5, Relaxation: 2, Chronic Fatigue: 3}
[log] ERROOROROROOROR ==== {Stress: 1, Involvement: 1, Slight Fatigue: 2, Anxiety: 5, Relaxation: 2, Chronic Fatigue: 3}
[log] _states ===== {Stress: [0, 0], Anxiety: [2, 5]}
states === [{externalSessionId: 19dd1148-97e3-4e33-8cd6-9d4ab3b8d64c, iaf: 11, iapf: 9, startTime: null, endTime: null, states: {Anxiety: 2, Stress: 1, Chronic Fatigue: 1}}, {externalSessionId: d1d6637a-a445-467d-b658-91e42b080ed6, iaf: 12, iapf: 8, startTime: null, endTime: null, states: {Stress: 1, Involvement: 1, Slight Fatigue: 2, Anxiety: 5, Relaxation: 2, Chronic Fatigue: 3}}]
[log] Stress ==== {Anxiety: 2, Stress: 1, Chronic Fatigue: 1}
[log] ERROOROROROOROR ==== {Anxiety: 2, Stress: 1, Chronic Fatigue: 1}
[log] Stress ==== {Stress: 1, Involvement: 1, Slight Fatigue: 2, Anxiety: 5, Relaxation: 2, Chronic Fatigue: 3}
[log] ERROOROROROOROR ==== {Stress: 1, Involvement: 1, Slight Fatigue: 2, Anxiety: 5, Relaxation: 2, Chronic Fatigue: 3}
[log] _states ===== {Stress: [0, 0], Anxiety: [2, 5]}
压力应为 [1,1]
如果我添加:
if (shortcut["Involvement"] != null) {
_states["Involvement"].add(shortcut["Involvement"]);
} else {
_states["Involvement"].add(0);
}
然后这种参与正常工作,如果增加放松然后放松像压力一样返回我的空错误..
【问题讨论】:
-
嘿,大个子,发生了什么我认为上次的答案是正确的
-
@Georgina,感谢您的回答,这对我帮助很大。但是这次我想找出为什么我的代码不起作用。此处的代码是在您回答之前编写的