0

I am getting two errors one in Quiz.dart and another in Main.dart.

In main.dart I am getting an error on answerQuestion: _answerQuestion,

Error: [The argument type 'void Function(int)' can't be assigned to the parameter type 'void Function()'.]

Main.dart

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

in quiz.dart I am getting an error on answerQuestion(answer['score']),

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

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

I have tried my best to understand the error, your assistance is highly appreciated, thanks in Advance.

5 Answers 5

2

The void _answerQuestion function expects an int parameter value but the class Quiz declares final VoidCallback answerQuestion and functions of the type VoidCallback doesn't expects any kind of parameter.

That is why you are getting The argument type 'void Function(int)' can't be assigned to the parameter type 'void Function()' error message. You can define your own type using typedef keyword or use ValueSetter from flutter foundation instead VoidCallback.

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

Or just replace VoidCallback answerQuestion by ValueSetter<int> answerQuestion in Quiz class.

1
  • Thank you very much, I changed the VoidCallback answerQuestion to Function answerQuestion & the issue got resolved. Actually I had an error previously when I declared it as function that is why I had changed it to VoidCallback. Still a beginner so has to fall a lot.
    – Allath
    Commented Jun 6, 2021 at 15:22
1

I have same problem but I corrected it. You should change the final VoidCallback answerQuestion;
as final Function answerQuestion;

It works

0

VoidCallback is a void Function() prototype but you pass to answerQuestion the void Function(int). Use, for example, ValueSetter<int>.

In regarding to second error check how you pass parameters to Answer constructor. I think you have named parameters (they are wrapped with curved parenthesis) but you pass them as positional.

0
0

Had the same issue, just change:

  • final VoidCallback answerQuestion; to
  • final Function answerQuestion; It worked for me like a charm
0
0

had the same issue for me i had:

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

the Solution was to remove the bracers from:

final Function antwortFrage;

Not the answer you're looking for? Browse other questions tagged or ask your own question.