我开始研究移动开发,有很多疑问和问题.我有课要上.我正在传递一个字段值"Score",但得到了一个错误.帮我理解一下.谢谢. 我的错误:类型‘()=>;Null’不是类型转换中类型‘(Int)=>;void’的子类型
这是主要的
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
static const _questions = [
{
'questionText': 'What\'s your favorite color?',
'answers': [
{'text': 'Red', 'score': 10},
{'text': 'Blue', 'score': 7},
{'text': 'Green', 'score': 5},
{'text': 'Yellow', 'score': 2},
],
},
{
'questionText': 'What do you do?',
'answers': [
{'text': 'Eat', 'score': 9},
{'text': 'Sleep', 'score': 10},
{'text': 'Drink water', 'score': 4},
{'text': 'Run', 'score': 3},
],
},
{
'questionText': 'Do you have chairs?',
'answers': [
{'text': 'Yes', 'score': 10},
{'text': 'No', 'score': 1},
{'text': 'A lot', 'score': 3},
{'text': 'I don\'t know', 'score': 7},
],
},
{
'questionText': 'Would you like some red wine?',
'answers': [
{'text': 'Yes', 'score': 1},
{'text': 'No', 'score': 9},
{'text': 'I don\'t drink alcohol', 'score': 10},
{'text': 'No, I would like soda', 'score': 7},
],
},
];
var _questionIndex = 0;
var _totalScore = 0;
void _answerQuiz(int score) {
_totalScore += score;
setState(() {
_questionIndex++;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'QuiZ',
home: Scaffold(
appBar: AppBar(
title: const Text('Quiz'),
),
body: _questionIndex < _questions.length
? Quiz(
questions: _questions,
questionIndex: _questionIndex,
answerQuestion: _answerQuiz,
)
: Result(_totalScore),
),
);
}
}
这是班级测验
class Quiz extends StatelessWidget {
final List<Map<String, Object>> questions;
final int questionIndex;
final void Function(int) answerQuestion;
// final VoidCallback answerQuestion;
const Quiz({
required this.questions,
required this.questionIndex,
required this.answerQuestion,
super.key,
});
@override
Widget build(BuildContext context) {
return Center(
child: Column(
children: [
Question(questions[questionIndex]['questionText'] as String),
...(questions[questionIndex]['answers'] as List<Map<String, Object>>)
.map((answer) {
return Answer(
answer['text'] as String,
() {
answerQuestion(answer['score'] as int);
} as void Function(int p1),
);
}).toList(),
],
),
);
}
}
这是班级的答案
class Answer extends StatelessWidget {
final String answerText;
final void Function(int) selectHandler;
// final VoidCallback selectHandler;
const Answer(this.answerText, this.selectHandler, {super.key});
@override
Widget build(BuildContext context) {
return ElevatedButton(
style: const ButtonStyle(
backgroundColor: MaterialStatePropertyAll(Colors.cyan),
foregroundColor: MaterialStatePropertyAll(Colors.blueAccent),
),
onPressed: () => selectHandler,
child: Text(answerText),
);
}
}
这节课的成绩
class Result extends StatelessWidget {
final int resultScore;
const Result(this.resultScore, {super.key});
String get resultPhrase {
var resultText = 'You did it';
if (resultScore <= 8) {
resultText = 'You are awesome and innocent';
} else if (resultScore <= 12) {
resultText = 'Pretty likeable';
} else if (resultScore <= 16) {
resultText = 'You are ... strange?!';
} else {
resultText = 'You are so bad!!!';
}
return resultText;
}
@override
Widget build(BuildContext context) {
return Center(
child: Text(resultPhrase),
);
}
}
我希望得到的值是"分数"