简体   繁体   English

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

[英]I am trying to make a quiz app in flutter with geeks for geeks tutorial as reference.. i have following errors

I followed the whole tutorial word to word, here is the link: https://www.geeksforgeeks.org/basic-quiz-app-in-flutter-api/我逐字逐句地学习了整个教程,这里是链接: https://www.geeksforgeeks.org/basic-quiz-app-in-flutter-api/

I have two errors我有两个错误

one in main:一个主要:

Result(_totalScore, _resetQuiz(context)) in this line it says: Result(_totalScore, _resetQuiz(context))在这一行中说:

This expression has a type of 'void' so its value can't be used.此表达式的类型为“void”,因此无法使用其值。 Try checking to see if you're using the correct API;尝试检查您是否使用了正确的 API; there might be a function or call that returns void you didn't expect.可能有一个 function 或返回 void 你没想到的电话。 Also, check type parameters and variables which might also be 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,
    );
  }
}

one errors in quiz.dart quiz.dart 中的一个错误

in the following line: (questions[questionIndex]['answers']) as List<Map<String, Object>>)在以下行中: (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()
      ],
      );
  }
}

errors: The element type 'Map<String, Object>' can't be assigned to the list type 'Widget'.错误:无法将元素类型“Map<String, Object>”分配给列表类型“Widget”。

The first error: you should do it like this Result(_totalScore, _resetQuiz) , you don't have to add a parameter because you are passing a function not to call the function itself.第一个错误:你应该这样做Result(_totalScore, _resetQuiz) ,你不必添加参数,因为你传递的是 function 而不是调用 function 本身。

The second error: it's a typo ...(questions[questionIndex]['answers'] as List<Map<String, Object>>)第二个错误:打错了...(questions[questionIndex]['answers'] as List<Map<String, Object>>)

For the first error Result method is returning void instead of widget.对于第一个错误,Result 方法返回的是 void 而不是 widget。 Check the function again.再次查看 function。 You might be missing a return statement.您可能缺少返回语句。 Same thing with the second error.与第二个错误相同。 Instead of returning widget the method is returning map<string, object>.该方法不返回小部件,而是返回 map<string, object>。 If that object is widget try accessing it using ["object name"].如果 object 是小部件,请尝试使用 ["object name"] 访问它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试制作一个测验应用程序以在 Flutter 和 Dart 中进行培训,并希望能帮助我清除以下错误 - I am trying to Make a Quiz app for training in Flutter and Dart, and would appreciate assistance in getting the following error cleared 在 Flutter 中,我正在尝试使用以下 json 创建依赖下拉列表 - In Flutter, I am trying to make a dependent dropdown with the following json 尝试运行 flutter 应用程序时出现错误。 参数的值不能为“null” - I am getting errors when trying to run flutter app. The parameter can't have a value of 'null' 我正在尝试运行我的 flutter 应用程序,但出现这些错误? - I am trying to run my flutter app and I am getting these errors? 我正在尝试在 flutter 应用程序中实现底部导航栏,但我的源代码中不断出现错误 - I am trying to implement a bottom navigation bar in a flutter app but I keep getting errors in my source code 我正在尝试使用 Flutter 制作计算器应用程序的副本 UI,但我遇到了一些问题 - I am trying to make a replica UI of a calculator app using Flutter and I am stuck in a few things 大家好,我是 flutter 和 dart 的新手,我需要帮助。 我正在学习教程,但遇到了这个问题。 它说“预计会找到','。” 在 bodyImage - hey guys am new to flutter and dart and I need help. I was following a tutorial and I have this problem. it says "Expected to find ','." at bodyImage 我正在尝试使用颤振制作 Bmi 计算器,但出现了很多错误 - I am trying to make the Bmi calculator using flutter but got many errors Firebase 快照文档。 我正在关注教程,但我不知道如何进行此更改 - Firebase snapshot documentations. I am following a tutorial but i can't figure out how to make this change 我正在尝试从 Flutter for app 中的实时数据库 firebase 中获取数据。 所以,我尝试了几种方法,但无法使其工作 - I am trying to fetch data from real-time database firebase in flutter for app. So, I have tried couple ways but couldn't make it work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM