我有一个问题,与空,我不知道为什么..我乞讨的Flutter
“对null调用了方法'map'。接收方:空尝试呼叫:Map(闭合:(字符串)=〉答案)”
main.dart
'
import 'package:flutter/material.dart';
import './question.dart';
import './answer.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return _MyAppState();
}
}
class _MyAppState extends State<MyApp> {
static const questions = [
{
'questionText': 'Whats your favourite car?',
'answers': ['HYUNDAI', 'BMW', 'KIA', 'AUDI']
},
{
'questionText': 'What is your favourite game?',
'answers': ['CS:GO', 'LOL', 'WOW', 'GTA']
},
{
'questionText': 'What is your favourite phone?',
'answer': ['IPHONE', 'SAMSUNG', 'SONY', 'HUAWEI']
}
];
var _questionIndex = 0;
void _answerQuestion() {
setState(() {
_questionIndex = _questionIndex + 1;
});
if (_questionIndex < questions.length) {
print('We have more questions');
}
print(_questionIndex);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.red,
title: Text('Quiz App'),
),
body: _questionIndex < questions.length
? Column(
children: [
Question(
questions[_questionIndex]['questionText'] as String,
),
...(questions[_questionIndex]['answers'] as List<String>)
.map((answer) {
return Answer(_answerQuestion, answer);
}).toList(),
],
)
: Center(child: Text('We finally got it'))),
);
}
}
question.dart
import 'package:flutter/material.dart';
class Question extends StatelessWidget {
final String questionText;
Question(this.questionText);
@override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
margin: EdgeInsets.all(10),
child: Text(
questionText,
style: TextStyle(fontSize: 22),
textAlign: TextAlign.center,
),
);
}
}
answer.dart
>
import 'package:flutter/material.dart';
class Answer extends StatelessWidget {
final VoidCallback selectHandler;
final String answerText;
Answer(this.selectHandler, this.answerText);
@override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
child: ElevatedButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.red),
),
onPressed: selectHandler,
child: Text(answerText),
));
}
}
'
enter image description here
请帮助我:)
我查找了关于此问题的不同主题,将??添加到toList(),但仍然出现相同的错误
1条答案
按热度按时间zvokhttg1#
在最后一项中,将
answers
替换为answer
。请看一下!