【发布时间】:2021-09-19 17:44:08
【问题描述】:
在这里,我正在尝试制作一个 Begginer 测验应用程序。 尝试为每个小部件创建一个单独的文件。 在这个过程中,使用回调函数出现错误,找不到解决方法。
'''
import 'package:flutter/material.dart';
import 'question.dart';
import 'answer.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _MyAppState();
}
}
class _MyAppState extends State<MyApp> {
var _questionIndex = 0;
void _answerQuestion() {
setState(() {
_questionIndex = _questionIndex + 1;
});
print(_questionIndex);
}
@override
Widget build(BuildContext context) {
var questions = [
'What\'s your choice of color ?',
'What\'s your choice of animal'
];
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: (Text('Quiz time')),
),
body: Column(
children: [
Question(
questions[_questionIndex],
),
Answer(_answerQuestion),
Answer(_answerQuestion),
Answer(_answerQuestion),
],
),
),
);
}
}
文件名= answer.dart 导入'package:flutter/material.dart';
class Answer extends StatelessWidget {
final Function selectHandler;
Answer(this.selectHandler);
@override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
child: ElevatedButton(
child: Text('Answer 1'),
onPressed:selectHandler,
**here in selectHandler it is showing error**
),
);
}
}
''' 参数类型“Function”不能分配给参数类型“void Function()?”。
【问题讨论】: