简体   繁体   English

Flutter 上位置 arguments 错误太多

[英]Error of too many positional arguments on Flutter

I am new on flutter and have been having an error of我是 flutter 的新用户,一直有一个错误

"Too many positional arguments: 0 expected, but 1 found.- Try removing the extra arguments."

I had no idea how to solve the problem.我不知道如何解决这个问题。 Here is my code.这是我的代码。

import 'package:flutter/material.dart';
import './question.dart';
import './answer.dart';

// ignore: must_be_immutable
class Quiz extends StatelessWidget {
  final List<Map<String, Object>> questions;
  final int questionIndex;
  final Function() answerQuestion;

  const Quiz(
      {required this.questions,
      required this.answerQuestion,
      required this.questionIndex});

  @override
 Widget build(BuildContext context) {
    return Column(
      children: [
        Question(
          questions[questionIndex]['questionText'] as String,
        ),
        ...(questions[questionIndex]['answers'] as List<Map<String, Object>>)
            .map((answer) {
          return Answer(() => answerQuestion****(**answer['score'] as int****)**,
              answer['text'] as String);
        }).toList(),
      ],
    );
  }
}

You have a function with no parameter, but you pass one.你有一个没有参数的 function,但你传递了一个。 For your code to work, your function must accept a positional parameter:为了让您的代码正常工作,您的 function 必须接受一个位置参数:

final Function(int) answerQuestion;

instead of代替

final Function() answerQuestion;

Optionally you can also work with named arguments:您还可以选择使用名为 arguments 的名称:

final Function({required int score}) answerQuestion;

and call it like this:并这样称呼它:

answerQuestion(score: answer['score'] as int)

your answerQuestion method expects no arguments.您的 answerQuestion 方法不需要 arguments。

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

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