繁体   English   中英

我正在尝试在 flutter 中制作一个测验应用程序,以极客教程作为参考。我有以下错误

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM