flutter - 我正在尝试制作一个与极客教程作为参考的极客应用程序。我有以下错误
问题描述
我按照整个教程逐字逐句,这是链接:https ://www.geeksforgeeks.org/basic-quiz-app-in-flutter-api/
我有两个错误
一个主要的:
Result(_totalScore, _resetQuiz(context))
在这一行中它说:
此表达式的类型为“void”,因此无法使用其值。尝试检查您是否使用了正确的 API;可能有一个函数或调用会返回您没想到的 void。此外,检查类型参数和变量也可能是无效的。
import 'package:flutter/rendering.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> {
final _questions = const [
{
'questionText': '1. ABC?',
'answers': [
{'text': 'E', 'score': 0},
{'text': 'F', 'score': 0},
{'text': 'D', 'score': 1},
{'text': 'S', 'score': 0},
],
},
{
'questionText': '1. ABC?',
'answers': [
{'text': 'E', 'score': 0},
{'text': 'F', 'score': 0},
{'text': 'D', 'score': 1},
{'text': 'S', 'score': 0},
],
},
{
'questionText': '1. ABC?',
'answers': [
{'text': 'E', 'score': 0},
{'text': 'F', 'score': 0},
{'text': 'D', 'score': 1},
{'text': 'S', 'score': 0},
],
},
{
'questionText': '1. ABC?',
'answers': [
{'text': 'E', 'score': 0},
{'text': 'F', 'score': 0},
{'text': 'D', 'score': 1},
{'text': 'S', 'score': 0},
],
},
];
var _questionIndex = 0;
var _totalScore = 0;
void _resetQuiz(BuildContext context) {
setState(() {
_questionIndex = 0;
_totalScore = 0;
});
}
void _answerQuestion(int score) {
_totalScore += score;
setState(() {
_questionIndex = _questionIndex + 1;
});
print(_questionIndex);
if (_questionIndex < _questions.length) {
print('We have more questions!');
} else {
print('No More Questions :(');
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Quiz'),
backgroundColor: Colors.black,
),
body: Padding(
padding: const EdgeInsets.all(30.0),
child: _questionIndex < _questions.length
? Quiz(
answerQuestion: _answerQuestion,
questionIndex: _questionIndex,
questions: _questions,
)
: Result(_totalScore, _resetQuiz(context)),
),
),
debugShowCheckedModeBanner: false,
);
}
}
quiz.dart 中的一个错误
在以下行中:(questions[questionIndex]['answers']) as List<Map<String, Object>>)
import './answers.dart';
import './questions.dart';
class Quiz extends StatelessWidget {
final List<Map<String, Object>> questions;
final int questionIndex;
final Function 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<Map<String, Object>>)
.map((answer)
{
return Answer(()=> answerQuestion(answer['score']), answer['text']
);
}).toList()
],
);
}
}
错误:无法将元素类型“Map<String, Object>”分配给列表类型“Widget”。
解决方案
第一个错误:你应该这样做Result(_totalScore, _resetQuiz)
,你不必添加参数,因为你传递一个函数而不是调用函数本身。
第二个错误:这是一个错字...(questions[questionIndex]['answers'] as List<Map<String, Object>>)
推荐阅读
- c - C中带有指针和结构数组的结构
- scala - 工件 ID 中包含的 Scala 和 Scala.js 版本
- mysql - 使用修剪功能缩小结果集
- database - Neo4j 节点属性数组来创建关系
- api - 当我将 Swagger API 导入 Postman 时,所有请求名称在 Postman GUI 中最终都是空白的
- mdriven - 在 MDriven Turnkey 中执行表达式 OnShow
- python - 将 skimage 图像传递给 cv2.threshold 函数
- reactjs - 如何在任何操作下重置(redux)reducer 状态值?
- jsp - Struts2 - 无法在 jsp 文件中显示字符串值
- mysql - CASE WHEN 多重条件,仅在移除另一个条件后才有效