【问题标题】:The argument type 'Object?' can't be assigned to the parameter type 'String' latest version参数类型“对象?”不能分配给参数类型'String'最新版本
【发布时间】:2021-09-24 18:17:48
【问题描述】:

她的部分来自 code.dart:

final List<Map< String,Object>> question = [
    {
      'questionText': 'what\'s your favorite\'s color?',
      'answers': [
        'Black',
        'Green',
        'Blue',
        'Yellow',
      ]
    },
    {
      'questionText': 'Select a true fact about Paulo Maldini!',
      'answers': [
        'He is dead',
        'He is alive',
        'He killed',
        'He is single',
      ]
    },
  ];

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            centerTitle: true,
            title: MyText("Quiz Bahlol", fontstyle2),
          ),
          body: Container(
            width: double.infinity,
            child: Column(
              children: <Widget>[
                Question(question[_questionIndex]['questionText']),
            // in this line error
   question[_questionIndex]['answers'].map((answer) {
                  return Answer(answerQuestion, answer);
                }).toList(),
              ],
            ),
          )),
    );

并在运行应用程序时给我这个错误:

需要一个“小部件”类型的值,但得到一个“列表”类型的值

但在旧版本中工作正常。 参数类型“对象?”不能分配给参数类型“字符串”。

【问题讨论】:

  • 请提供完整的错误信息。它还应该解释问题出在哪一行。
  • 如果您的代码还没有准备好进行更严格的空安全类型检查,请不要启用空安全。将 pubspec.yaml 中的 Dart SDK 最低版本设置为 2.12 之前的版本。
  • 展示你的问答类

标签: flutter dart


【解决方案1】:

你应该试试这个:

改变这个:

final List<Map< String,Object>> question = [
{
  'questionText': 'what\'s your favorite\'s color?',
  'answers': [
    'Black',
    'Green',
    'Blue',
    'Yellow',
  ]
},
{
  'questionText': 'Select a true fact about Paulo Maldini!',
  'answers': [
    'He is dead',
    'He is alive',
    'He killed',
    'He is single',
  ]
},

];

到:

var question = [
    {
      'questionText': 'what\'s your favorite\'s color?',
      'answers': [
        'Black',
        'Green',
        'Blue',
        'Yellow',
      ]
    },
    {
      'questionText': 'Select a true fact about Paulo Maldini!',
      'answers': [
        'He is dead',
        'He is alive',
        'He killed',
        'He is single',
      ]
    },
  ];

还有变化:

Question(question[_questionIndex]['questionText']),

到:

Question(question[_questionIndex]['questionText'] as String),

【讨论】:

    【解决方案2】:

    问题出在您的Column 小部件中。您应该使用

    Container(
      width: double.infinity,
      child: Column(
        children: <Widget>[
          Question(question[_questionIndex]['questionText']),
          ...question[_questionIndex]['answers'].map((answer) {
            return Answer(answerQuestion, answer);
          }),
        ],
      ),
    )
    
    Container(
      width: double.infinity,
      child: Column(
        children: <Widget>[
          Question(question[_questionIndex]['questionText']),
          for (var answer in question[_questionIndex]['answers'])
            Answer(answerQuestion, answer)
        ],
      ),
    )
    

    【讨论】:

      【解决方案3】:

      您的列的子列是使用列表中的列表创建的,它应该只是一个列表。

      只需少量代码编辑的最简单解决方案:

      children: <Widget>[
         Question(question[_questionIndex]['questionText']),
       ] + List.generate(question[_questionIndex]['answers'].length, (index) => 
          Answer(answerQuestion, answer))
             
       
      

      【讨论】:

        猜你喜欢
        • 2021-11-06
        • 2021-10-02
        • 2021-08-17
        • 2021-10-06
        • 2020-04-15
        • 2021-10-24
        • 2021-06-19
        • 2021-08-19
        • 2021-11-14
        相关资源
        最近更新 更多