我有一張看起來像這樣的地圖
Map<String, String> vaccinated = {
'dog': 'ready',
'cat': 'ready',
'mouse': 'done',
'horse': 'done',
};
我只想計算地圖內的“完成”并希望得到結果
2
但我試過的東西只是布林值 任何人都知道我如何計算地圖中的特定值
uj5u.com熱心網友回復:
你可以這樣做
void main() {
Map<String, String> vaccinated = {
'dog': 'ready',
'cat': 'ready',
'mouse': 'done',
'horse': 'done',
};
final total =
vaccinated.entries.where((e) => e.value == "done").toList().length;
print(total);
}
更多關于地圖
uj5u.com熱心網友回復:
您可以折疊地圖的值以獲取計數。
Map<String, String> vaccinated = {
'dog': 'ready',
'cat': 'ready',
'mouse': 'done',
'horse': 'done',
};
int count = vaccinated.values.fold(0, (value, current) => value (current == 'done' ? 1 : 0));
print(count);
查看折疊方法:https : //api.flutter.dev/flutter/dart-core/Iterable/fold.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/402075.html
上一篇:在網格視圖中縮短字串值
下一篇:Getx4.6.1的問題
