flutter - 我正在尝试制作一个用于 Flutter 和 Dart 培训的测验应用程序,希望能帮助我清除以下错误
问题描述
我在 Quiz.dart 和 Main.dart 中遇到两个错误。
在 main.dart 我收到answerQuestion: _answerQuestion错误,
错误:[无法将参数类型“void Function(int)”分配给参数类型“void Function()”。]
主要.dart
import 'package:flutter/material.dart';
import './quiz.dart';
import './result.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _MyAppState();
}
}
class _MyAppState extends State<MyApp> {
static const _questions = const [
{
'questionText': 'What\'s your favorite Color?',
'answers': [
{'text': 'Red', 'score': 10},
{'text': 'White', 'score': 5},
{'text': 'Black', 'score': 3},
{'text': 'Green', 'score': 1},
],
},
{
'questionText': 'What\'s your favorite animal?',
'answers': [
{'text': 'Dog', 'score': 10},
{'text': 'Cat', 'score': 7},
{'text': 'Monkey', 'score': 5},
{'text': 'Tiger', 'score': 2},
],
},
{
'questionText': 'What\'s your favorite Number',
'answers': [
{'text': 'Raman', 'score': 10},
{'text': 'Krishnan', 'score': 7},
{'text': 'Balan', 'score': 5},
{'text': 'Gopi', 'score': 2},
],
},
];
var _questionIndex = 0;
var _totalScore = 0;
void _answerQuestion(int score) {
_totalScore += score;
setState(() {
_questionIndex = _questionIndex + 1;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('My First App'),
),
body: _questionIndex < _questions.length
? Quiz(
answerQuestion: _answerQuestion,
questionIndex: _questionIndex,
questions: _questions,
)
: Result(_totalScore),
));
}
}
在 quiz.dart 我收到answerQuestion(answer['score'])错误,
错误:位置参数太多:预期为 0,但找到了 1。尝试删除额外的参数。
测验.dart
import 'package:flutter/material.dart';
import './answer.dart';
import './question.dart';
class Quiz extends StatelessWidget {
// final List<Map<String, Object>> questions;
final List<Map<String, dynamic>> questions;
final int questionIndex;
final VoidCallback answerQuestion;
Quiz({
required this.questions,
required this.answerQuestion,
required this.questionIndex,
});
@override
Widget build(BuildContext context) {
return Column(
children: [
Question(
questions[questionIndex]['questionText'],
),
// ...(questions[questionIndex]['answers'] as List<String>).map((answer) {
...(questions[questionIndex]['answers'] as List<Map<String, dynamic>>)
.map((answer) {
return Answer(() => answerQuestion(answer['score']), answer['text']);
}).toList()
],
);
}
}
我已尽力了解该错误,非常感谢您的帮助,在此先感谢。
解决方案
该void _answerQuestion
函数需要一个int
参数值,但该类Quiz
声明final VoidCallback answerQuestion
并且该类型的函数VoidCallback
不需要任何类型的参数。
这就是为什么您收到参数类型“void Function(int)”无法分配给参数类型“void Function()”错误消息的原因。typedef
您可以使用关键字定义自己的类型,也可以使用ValueSetter
Flutter Foundation 来代替VoidCallback
。
typedef MyOwnFunction = void Function(int score);
class Quiz extends StatelessWidget {
//...
final MyOwnFunction answerQuestion;
//...
}
或者只是在课堂上替换VoidCallback answerQuestion
。ValueSetter<int> answerQuestion
Quiz
推荐阅读
- javascript - JavaScript 中的 PHP 破坏了所有脚本
- sql - Could EXEC and Select Into work together?
- c++ - Visual Studio 2012 和 Visual Studio 2015 的双重结果不同
- c# - 如果 mysql 数据库中存在,则禁用 wpf 组合框的项目
- android - AOSP 时出现问题 – 创建系统服务
- css - 页脚显示错误响应式设计
- c# - 具有弹性搜索目标的 Nlog ASP.Net Core 不起作用
- angular - 如何将数组动态绑定到 ag-grid 列
- react-native - Clojure 上的 React-native - 通过 id 获取 ScrollView 并在 ScrollView 内滚动到视图
- javascript - 如何在本地存储中存储完整的对象