gpt4 book ai didi

dart - 使用字符串插值访问 dartlang 中嵌套映射中的值

转载 作者:IT王子 更新时间:2023-10-29 06:47:48 27 4
gpt4 key购买 nike

我有以下代码,我正在尝试使用字符串插值来访问嵌套的 map 值。但是,它返回整个变量而不仅仅是一个特定的值。

Map<String, Map<String, String>> questionBank = {
"1": {
"question": "What is the capital of Canada?",
"ans1": "Toronto",
"ans2": "Montreal",
"ans3": "Ottawa",
"ans4": "Vancouver"
},
"2": {
"question": "What is the capital of Britain?",
"ans1": "London",
"ans2": "Manchester",
"ans3": "Newcastle",
"ans4": "Edinburgh"
}
};

void main() {
print('Question: $questionBank[1][question]');
}

我该如何解决这个问题?感谢任何见解。

最佳答案

显然,它的计算结果是一个表达式,而不仅仅是一个变量,因此需要用括号括起来。

print('Question: ${questionBank["1"]["question"]}');

关于dart - 使用字符串插值访问 dartlang 中嵌套映射中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43839033/

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