我正试图使一个测验应用程序的培训,在Flutter和 dart ,并希望协助获得以下错误清除

kmynzznz  于 2023-03-27  发布在  Flutter
关注(0)|答案(5)|浏览(117)

我得到两个错误,一个在Quiz.dart,另一个在Main.dart。
在main.dart中,我在answerQuestion上遇到错误:_answerQuestion

错误:[参数类型'void Function(int)'无法分配给参数类型'void Function()'。]
主省道

import 'package:flutter/material.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> {
  static const _questions = const [
    {
      'questionText': 'What\'s your favorite Color?',
      'answers': [
        {'text': 'Red', 'score': 10},
        {'text': 'White', 'score': 5},
        {'text': 'Black', 'score': 3},
        {'text': 'Green', 'score': 1},
      ],
    },
    {
      'questionText': 'What\'s your favorite animal?',
      'answers': [
        {'text': 'Dog', 'score': 10},
        {'text': 'Cat', 'score': 7},
        {'text': 'Monkey', 'score': 5},
        {'text': 'Tiger', 'score': 2},
      ],
    },
    {
      'questionText': 'What\'s your favorite Number',
      'answers': [
        {'text': 'Raman', 'score': 10},
        {'text': 'Krishnan', 'score': 7},
        {'text': 'Balan', 'score': 5},
        {'text': 'Gopi', 'score': 2},
      ],
    },
  ];
  var _questionIndex = 0;
  var _totalScore = 0;

  void _answerQuestion(int score) {
    _totalScore += score;
    setState(() {
      _questionIndex = _questionIndex + 1;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
      appBar: AppBar(
        title: Text('My First App'),
      ),
      body: _questionIndex < _questions.length
          ? Quiz(
              answerQuestion: _answerQuestion,
              questionIndex: _questionIndex,
              questions: _questions,
            )
          : Result(_totalScore),
    ));
  }
}

在quist.dart中,我在**answerQuestion(answer['score'])**上得到一个错误,

错误:位置参数太多:应为0,但找到1。请尝试删除多余的参数。
**竞猜 dart **

import 'package:flutter/material.dart';

import './answer.dart';
import './question.dart';

class Quiz extends StatelessWidget {
  // final List<Map<String, Object>> questions;
  final List<Map<String, dynamic>> questions;
  final int questionIndex;
  final VoidCallback 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<String>).map((answer) {
        ...(questions[questionIndex]['answers'] as List<Map<String, dynamic>>)
            .map((answer) {
          return Answer(() => answerQuestion(answer['score']), answer['text']);
        }).toList()
      ],
    );
  }
}

我已经尽我所能去理解这个错误,非常感谢你的帮助,提前谢谢。

ykejflvf

ykejflvf1#

void _answerQuestion函数需要一个int参数值,但类Quiz声明了final VoidCallback answerQuestion,而VoidCallback类型的函数不需要任何类型的参数。
这就是为什么你会收到**参数类型“void Function(int)”不能分配给参数类型“void Function()”**错误消息。你可以使用typedef关键字定义自己的类型,或者使用flutter foundation中的ValueSetter代替VoidCallback

typedef MyOwnFunction = void Function(int score);
class Quiz extends StatelessWidget {
    //...
    final MyOwnFunction answerQuestion;
    //...
}

或者在Quiz类中将VoidCallback answerQuestion替换为ValueSetter<int> answerQuestion

vngu2lb8

vngu2lb82#

VoidCallback是一个void Function()原型,但您将void Function(int)传递给answerQuestion。例如,使用ValueSetter<int>
关于第二个错误检查如何将参数传递给Answer构造函数,我认为你已经命名了参数(它们用圆括号括起来),但是你将它们作为位置参数传递。

63lcw9qa

63lcw9qa3#

我有同样的问题,但我纠正了它。你应该改变final VoidCallback answerQuestion;
作为final Function answerQuestion;
很管用

fiei3ece

fiei3ece4#

有同样的问题,只是改变:

  • final VoidCallback answerQuestion;
  • final Function answerQuestion;它对我来说就像一种魅力
dsekswqp

dsekswqp5#

对我来说也有同样的问题:

class Quiz extends StatelessWidget {
  final List<Map<String, Object>> fragen;
  final int frageIndex;
  final Function() antwortFrage;

解决方案是从以下方面移除支撑:

final Function antwortFrage;

相关问题