dart 错误:参数类型“List< String>”不能分配给参数类型“String”

ufj5ltwl  于 2023-01-28  发布在  其他
关注(0)|答案(4)|浏览(194)

我是dart的初学者,我正在尝试制作一个问答游戏应用程序,我使用一些小部件在屏幕上获取问题和答案选项。我遇到的错误是**"参数类型'List'不能分配给参数类型'String'"**以下是我的一些代码片段:-
1.这是一个Map〈String,Object〉的列表,我在这个变量中存储问题和答案选项。

var questions = [
      {
        'questionText': 'What is your favourite season?',
        'answers': [
          'Summer',
          'Monsoon',
          'Winter',
        ]
      },
      {
        'questionText': 'What is your favourite color?',
        'answers': [
          'Black',
          'Green',
          'Blue',
        ]
      },
      {
        'questionText': 'What is your favourite food?',
        'answers': [
          'Chicken',
          'Kebab',
          'Biryani',
        ]
      },
    ];

1.这是我调用问答小部件的地方。

body: Column(
          children: [
            Question(questions[_questionsIndex]["questionText"]),
            ...{questions[_questionsIndex]['answers'] as List<String>}
                .map((answer) {
              return Answer(_answerQuestion, answer); //**ERROR**
            }).toList(),
          ],
        ),

错误出现在语句中:return答案(_answerQuestion,答案);
1.以下是我的应答小部件类的外观:

class Answer extends StatelessWidget {
  final Function selectAnswer;
  final String answerText;

  Answer(this.selectAnswer, this.answerText);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      child: ElevatedButton(
        style: ElevatedButton.styleFrom(
          primary: Colors.blue,
          elevation: 0.0,
        ),
        child: Text(answerText,
            style: TextStyle(
              color: Colors.black,
            )),
        onPressed: () {
          selectAnswer();
        },
      ),
    );
  }
}
    • 此错误的含义是什么?为什么会出现?如何修复此错误?**

谢谢大家!

ttvkxqim

ttvkxqim1#

如果您意思是:参数类型“List〈String?〉?”不能赋给参数类型“List〈String?〉”。
我用一行代码解决了这个问题,你可以从List〈String?〉?

List<String?>? mylist = ['data1','data2'];
List<String?> mylist2 = List<String>.from(mylist);
j8yoct9x

j8yoct9x2#

我认为问题出在你的第二个代码块中。对于字符串列表,你应该只使用List类型而不是List<String>类型。
你的代码应该是:

body: Column(
          children: [
            Question(questions[_questionsIndex]["questionText"]),
            ...{questions[_questionsIndex]['answers'] as List}
                .map((answer) {
              return Answer(_answerQuestion, answer); //**ERROR**
            }).toList(),
          ],
        ),

希望这能解决你的问题。

yptwkmov

yptwkmov3#

您可以尝试使用List<Map<String,dynamic>>指定主列表的类型

List<Map<String,dynamic>> questions = [
  {
    'questionText': 'What is your favourite season?',
    'answers': [
      'Summer',
      'Monsoon',
      'Winter',
    ]
  },
  {
    'questionText': 'What is your favourite color?',
    'answers': [
      'Black',
      'Green',
      'Blue',
    ]
  },
  {
    'questionText': 'What is your favourite food?',
    'answers': [
      'Chicken',
      'Kebab',
      'Biryani',
    ]
  },
];

我试着将元素赋值给List<String>,一切正常

var a = questions[1]['answers'];
List<String> b = a;
print(b);
nimxete2

nimxete24#

而不是使用列表改变它为var,它为我工作,错误消失是动态的,工作完美。这是我的代码如下:
类_QuizPageState扩展状态{
var scoreKeeper =[图标(图标检查,颜色:www.example.com,),图标(关闭图标,颜色:Colors.green www.example.com,),图标(图标,勾选,颜色: www.example.com,),图标(关闭图标,颜色:Colors.red, ), Icon( Icons.close, color:Colors.red, ), Icon( Icons.check, color:Colors.green, ), Icon( Icons.close, color:Colors.red, ),
];
@override小部件构建(构建上下文上下文){return列(mainAxisAlignment:主轴对中、间距、交叉轴对中:交叉轴对齐.拉伸,子项:[常量扩展(灵活:5,子项:填充(填充:边缘插入。全部(10.0),子对象:中心(子级:Text('这是问题文本的位置。',textAlign:www.example.com,样式:文本样式(字体大小:TextAlign.center颜色.白色、)、)、)、)、)、)、扩展(子项:填充(填充:边缘插入。全部(15.0),子对象:文本按钮(按下时:(){ TextButton( onPressed: (){

},
          child: Text('True'),
          // color: Colors.white,
          style: TextButton.styleFrom(
            backgroundColor: Colors.white,
          ),
        ),
      ),
    ),
    Expanded(
      child: Padding(
        padding: EdgeInsets.all(15.0),
        child:TextButton(
          onPressed: (){

          },
          // color: Colors.white,
          style: TextButton.styleFrom(
            backgroundColor: Colors.pinkAccent,
          ),
          child: const Text('False'),
        ),
      ),
    ),
    Row(
      children: scoreKeeper,
    ),
  ],
);

}}

相关问题