![](/img/trans.png)
[英]I am trying to Make a Quiz app for training in Flutter and Dart, and would appreciate assistance in getting the following error cleared
[英]I am trying to make a quiz app in flutter with geeks for geeks tutorial as reference.. i have following errors
我逐字逐句地學習了整個教程,這里是鏈接: https://www.geeksforgeeks.org/basic-quiz-app-in-flutter-api/
我有兩個錯誤
一個主要:
Result(_totalScore, _resetQuiz(context))
在這一行中說:
此表達式的類型為“void”,因此無法使用其值。 嘗試檢查您是否使用了正確的 API; 可能有一個 function 或返回 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)
,你不必添加參數,因為你傳遞的是 function 而不是調用 function 本身。
第二個錯誤:打錯了...(questions[questionIndex]['answers'] as List<Map<String, Object>>)
對於第一個錯誤,Result 方法返回的是 void 而不是 widget。 再次查看 function。 您可能缺少返回語句。 與第二個錯誤相同。 該方法不返回小部件,而是返回 map<string, object>。 如果 object 是小部件,請嘗試使用 ["object name"] 訪問它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.