gpt4 book ai didi

flutter - 如何在 map 中添加特定变量的值

转载 作者:行者123 更新时间:2023-12-03 04:39:48 24 4
gpt4 key购买 nike

我正在构建一个flutter应用程序,该应用程序从用户那里获取一些值并将其存储在 map 中。我使用pageviewbuilder来呈现用户应输入的项目,如下所示:

                    RadioListTile(
title: Text('${outcomeScore[index].sectionOne}'),
value: 1,
groupValue: selectedScore,
activeColor: Colors.blue,
onChanged: (val){
setSelectedScore(val);
setState(() {
scoreText = outcomeScore[index].sectionOne;
});
print(val);
print(scoreText);
},
secondary: Text("One"),
),

RadioListTile(
title: Text('${outcomeScore[index].sectionTwo}'),
value: 2,
groupValue: selectedScore,
activeColor: Colors.blue,
onChanged: (val){
setSelectedScore(val);
setState(() {
scoreText = outcomeScore[index].sectionTwo;
});
print(val);
print(scoreText);
},
secondary: Text("One"),
)
在PageView上,我使用next按钮移至下一页。下一个按钮将所选值添加到 map ,如下所示:
  Map<String, String> myMap= {
'createdOn' : '$currentTime',
};

myMap[outcomeScore[index].itemTitle] = scoreText;
myMap[outcomeScore[index].itemTitle + " Score"] = selectedScore.toString();
完成所有这些操作后,我最终得到了一个具有如下值的 map :
createdOn: 2020-08-17 10:55:22.983934, TestOne: VFR, TestOne Score: 3, TestTwo: TSA, TestTwo Score: 4, TestThree: RTA, TestThree Score: 3,
这就是 map 在Json上的样子
{

"TestOne" : "VFR",
"TestOne Score" : "3",
"TestTwo" : "TSA",
"TestTwo Score" : "4",
"TestThree" : "RTA",
"TestThree Score" "3"
}
在上一个综合浏览量上,我有一个“提交”按钮,而不是下一个,在此,我想在变量末尾加上“分数”来总结变量的所有值。有关如何执行此操作的任何想法?我将非常感谢您的帮助。
谢谢。

最佳答案

使用foreach循环遍历 map

var sum = 0;
map.forEach((key, value) {
if(key.contains('Score')){
//caution, throws if fails to parse
sum += int.parse(value);
}
});

关于flutter - 如何在 map 中添加特定变量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63447226/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com