参数类型'Object?'无法分配给参数类型"字符串"最新版本



Hers是来自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(),
],
),
)),
);

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

需要类型为"widget"的值,但得到类型为"List"的

但在旧版本中运行正常。参数类型"Object?"无法分配给参数类型"String"。

你应该试试这个:

更改此项:

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),

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

  • 答案窗口小部件之前的排列运算符:
Container(
width: double.infinity,
child: Column(
children: <Widget>[
Question(question[_questionIndex]['questionText']),
...question[_questionIndex]['answers'].map((answer) {
return Answer(answerQuestion, answer);
}),
],
),
)
  • 语句的集合,以便在Column子级中插入答案的小部件:
Container(
width: double.infinity,
child: Column(
children: <Widget>[
Question(question[_questionIndex]['questionText']),
for (var answer in question[_questionIndex]['answers'])
Answer(answerQuestion, answer)
],
),
)

您的列的子级是用List中的List创建的,实际上它应该只有一个List。

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

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


如果您使用的是更新的sdk版本的dart,则添加"作为字符串"回答您的问题''问题(问题[_questionIndex]['questionText'](,

将其更改为'Question(Question[_questionIndex]['questionText']'为字符串,''(,

但更重要的是,将按钮中的文本更改为"扫描文本

相关内容

最新更新