【问题标题】:The argument type 'Object?' can't be assigned to the parameter type 'int'/ 'string'参数类型“对象?”不能分配给参数类型'int'/'string'
【发布时间】:2021-11-06 00:24:37
【问题描述】:

我正在学习一个教程,但我在地图上出现错误我不知道为什么,在我看来我做得对

这是我的清单

var questions = [
    {
      'questiontext': "Questão 1",
      'answer': [
        {'text': 'Black', 'score': 10},
        {'text': 'Red', 'score': 5},
        {'text': 'Green', 'score': 3},
        {'text': 'White', 'score': 1},
      ]
    },
    {
      'questiontext': "Questão 2",
      'answer': {
        {'text': 'Rabbit', 'score': 3},
        {'text': 'Snake', 'score': 11},
        {'text': 'Elephant', 'score': 5},
        {'text': 'Lion', 'score': 9},
      },
    },
    {
      'questiontext': "Questão 3",
      'answer': {
        {'text': 'Peter', 'score': 1},
        {'text': 'Paul', 'score': 1},
        {'text': 'Max', 'score': 1},
      },
    }
  ];

我的飞镖代码

  final List<Map<String, Object>> questions;
        
...(questions[0]['answer'] as List<Map<String, Object>>)
            .map((answer) {
          return Answer(() => textfunction(answer['score']), answer['text']);
        }).toList()

错误

参数类型“对象?”不能分配给参数类型 'String'.dart(argument_type_not_assignable)

参数类型“对象?”不能分配给参数类型 'int'.dart(argument_type_not_assignable)

我不知道怎么了。我已经尝试切换到动态,它一直报错

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    尝试这样做: answer['score'] as int & answer['text'] as String

    【讨论】:

    • 感谢您的回复,但返回此错误type '_CompactLinkedHashSet&lt;Map&lt;String, Object&gt;&gt;' is not a subtype of type 'List&lt;Map&lt;String, Object&gt;&gt;' in type cast
    • 还要检查@w461 所说的,似乎也是一个问题。
    【解决方案2】:

    不确定这是否是问题所在,但答案 1 是一个列表,其他是集合 [ ] vs { }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-12
      • 2021-10-02
      • 2021-08-17
      • 2021-11-28
      • 2020-06-21
      • 2021-10-06
      • 2020-04-15
      • 2021-10-24
      相关资源
      最近更新 更多