Home > Blockchain >  Error of too many positional arguments on Flutter
Error of too many positional arguments on Flutter

Time:04-21

I am new on flutter and been having an error of "Too many positional arguments: 0 expected, but 1 found.- Try removing the extra arguments." on the bold part of my code for the past few days and I have no idea how to solve it.

enter code here

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(),
      ],
    );
  }
}

CodePudding user response:

You have a function with no parameter, but you pass one. For your code to work, your function must accept a positional parameter:

final Function(int) answerQuestion;

instead of

final Function() answerQuestion;

Optionally you can also work with named arguments:

final Function({required int score}) answerQuestion;

and call it like this:

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

CodePudding user response:

your answerQuestion method expects no arguments.

  • Related