首页 > 解决方案 > 我正在尝试制作一个与极客教程作为参考的极客应用程序。我有以下错误

问题描述

我按照整个教程逐字逐句,这是链接: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”。

标签: flutterlistview

解决方案


第一个错误:你应该这样做Result(_totalScore, _resetQuiz),你不必添加参数,因为你传递一个函数而不是调用函数本身。

第二个错误:这是一个错字...(questions[questionIndex]['answers'] as List<Map<String, Object>>)


推荐阅读