飞镖:ambiguous_set_or_map_literal_both

时间:2020-05-25 22:07:35

标签: flutter dart

您好,我是不熟悉移动应用程序开发并正在接受udemy课程的新手。 我陷入了困境,讲师可以同时使用maps和文字变量创建变量,而在我的编辑器中却遇到了这样的错误

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

检查屏幕截图以获取更多错误信息。

1 个答案:

答案 0 :(得分:2)

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}
      ]
    },
  ];