您好,我是 Flutter 移动应用程序开发新手,正在学习 udemy 类(class)。 我陷入了一个困境,即讲师可以使用 map 和文字变量创建变量,而在我的编辑器中我收到此错误,上面写着
This literal contains both 'Map' and 'Iterable' spreads, which makes it impossible to determine whether the literal is a map or a set. Try removing or changing some of the elements so that all of the elements are consistent
我和导师一起检查了代码很多次,也检查了 Dart 官方文档,但无法解决它。如果有人可以帮助我,请。 谢谢。
final _questions = const [
{
'questionText':
'Who is the current president of The United Stated of America?',
'answers': [
{'text': 'Donald Trump', 'score': 10},
{'text': 'Ram Nath Kovind', 'score': -10},
{'text': 'Pedro Sánchez', 'score': -10},
],
},
{
'questionText': 'Which of these is the currency used in Japan?',
'answers': {'text': 'Ringgit', 'score': -10},
{'text': 'Yuan', 'score': -10},
{'text': 'Yen', 'score': 10},
},
{
'questionText': 'Which of these is the capital of Spain?',
'answers': {'text': 'Madrid', 'score': 10},
{'text': 'Washington DC', 'score': -10},
{'text': 'Moscow', 'score': -10},
},
{
'questionText': 'Which one is the language of Israel?',
'answers': {'text': 'Persian', 'score': -10},
{'text': 'Hebrew', 'score': 10},
{'text': 'Turkish', 'score': -10},
},
{
'questionText': 'Who among these was the Missile Man of India?',
'answers': {'text': 'Pranab Mukherjee', 'score': -10},
{'text': 'Sir APJ Abdul Kalam', 'score': 10},
{'text': 'Bhagat Singh', 'score': -10},
},
];
这是包含一些问题集的变量。 Screenshot of code
检查屏幕截图以获取更多错误信息。
最佳答案
answers
第一个元素是数组,接下来的元素语法错误
这应该有效
final _questions = const [
{
'questionText':
'Who is the current president of The United Stated of America?',
'answers': [
{'text': 'Donald Trump', 'score': 10},
{'text': 'Ram Nath Kovind', 'score': -10},
{'text': 'Pedro Sánchez', 'score': -10},
],
},
{
'questionText': 'Which of these is the currency used in Japan?',
'answers': [
{'text': 'Ringgit', 'score': -10},
{'text': 'Yuan', 'score': -10},
{'text': 'Yen', 'score': 10}
]
},
{
'questionText': 'Which of these is the capital of Spain?',
'answers': [
{'text': 'Madrid', 'score': 10},
{'text': 'Washington DC', 'score': -10},
{'text': 'Moscow', 'score': -10}
]
},
{
'questionText': 'Which one is the language of Israel?',
'answers': [
{'text': 'Persian', 'score': -10},
{'text': 'Hebrew', 'score': 10},
{'text': 'Turkish', 'score': -10}
]
},
{
'questionText': 'Who among these was the Missile Man of India?',
'answers': [
{'text': 'Pranab Mukherjee', 'score': -10},
{'text': 'Sir APJ Abdul Kalam', 'score': 10},
{'text': 'Bhagat Singh', 'score': -10}
]
},
];
关于flutter - Dart : ambiguous_set_or_map_literal_both,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62011504/