flutter - Dart : ambiguous_set_or_map_literal_both

标签 flutter dart

您好,我是 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/

相关文章:

dart - 是否可以要求下拉选择作为 Flutter 中表单验证的一部分?

flutter - didChangeAppLifecycleState 没有按预期工作

android - 使用带有状态和构建参数的BottomNavigation

sorting - 按两个属性分别对List <object>进行排序,其中一个属性以升序排列,另一个属性以降序排列

dart - 运行测试时加载 dartium 失败

flutter - 找不到插件项目:location_web。请更新settings.gradle

dart - Flutter - 如何使用二进制流从服务器下载文件

flutter - 如何从异步/等待函数返回 bool 值并将其传递给其他页面中的其他变量

flutter - 如何在 flutter 中重新创建单个小部件并使其失去状态?

flutter - 在 dart 中操作 StackTrace