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



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

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"]}');

最新更新