我是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();
},
),
);
}
}
- 此错误的含义是什么?为什么会出现?如何修复此错误?**
谢谢大家!
4条答案
按热度按时间ttvkxqim1#
如果您意思是:参数类型“List〈String?〉?”不能赋给参数类型“List〈String?〉”。
我用一行代码解决了这个问题,你可以从List〈String?〉?
j8yoct9x2#
我认为问题出在你的第二个代码块中。对于字符串列表,你应该只使用
List
类型而不是List<String>
类型。你的代码应该是:
希望这能解决你的问题。
yptwkmov3#
您可以尝试使用
List<Map<String,dynamic>>
指定主列表的类型我试着将元素赋值给
List<String>
,一切正常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: (){
}}