mjj
mjj

Reputation: 27

flutter - The argument type 'void Function(int)' can't be assigned to the parameter type 'void Function()'\

Hi I'm learning flutter via a tutorial. In that tutorial they downgraded the dart version but I'm trying to work without downgrading the dart. I'm getting two errors The argument type 'void Function(int)' can't be assigned to the parameter type 'void Function()'.dartargument_type_not_assignable & Too many positional arguments: 0 expected, but 1 found. I'm trying to figure out how to solve that error.

    **main.dart file**
    
    class _MyAppState extends State<MyApp> {
      final _questions = const [
        {
          "question": "What's your favourite color?",
          "answers": [
            {"text": "Red", "score": 10},
            {"text": "Green", "score": 5},
            {"text": "Blue", "score": 3},
            {"text": "White", "score": 1}
          ],
        },
        {
          "question": "What's your favourite animal?",
          "answers": [
            {'text': 'Rabbit', 'score': 3},
            {'text': 'Snake', 'score': 11},
            {'text': 'Elephant', 'score': 5},
            {'text': 'Lion', 'score': 9},
          ],
        },
        {
          "question": "What's your favourite food?",
          "answers": [
            {'text': 'Biryani', 'score': 3},
            {'text': 'Dosa', 'score': 11},
            {'text': 'Idly', 'score': 5},
            {'text': 'Poori', 'score': 9},
          ],
        },
      ];
      var _questionIndex = 0;
      var _totalScore = 0;
    
        void _answerQuestion(int score) {
        _totalScore += score;
        setState(() {
          _questionIndex = _questionIndex + 1;
        });
        
      }
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          home: Scaffold(
            appBar: AppBar(
              title: const Text("My First App"),
            ),
            body: _questionIndex < _questions.length
                ? Quiz(
                    answerQuestion: _answerQuestion,//The argument type 'void Function(int)' can't be assigned to the parameter type 'void Function()'.dartargument_type_not_assignable
                    questionIndex: _questionIndex,
                    questions: _questions,
                  )
                : const Result(),
          ),
        );
      }
    }
**quiz.dart file**
class Quiz extends StatelessWidget {
  final List<Map<String, Object>> questions;
  final VoidCallback answerQuestion;
  dynamic questionIndex;
  Quiz(
      {required this.questions,
      required this.answerQuestion,
      required this.questionIndex,
      super.key});

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Question(questions[questionIndex]["question"] as String),
        ...(questions[questionIndex]["answers"] as List<Map<String, Object>>)
            .map(
              (answer) => Answer(() => answerQuestion(answer["score"]),//Too many positional arguments: 0 expected, but 1 found.

               answer["text"] as String),
            )
            .toList()
      ],
    );
  }
}
**answer.dart file**
class Answer extends StatelessWidget {
  final VoidCallback? selectHandler;
  final String? answerText;
  const Answer(this.selectHandler, this.answerText, {super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      child: ElevatedButton(
        style: ElevatedButton.styleFrom(
          backgroundColor: Colors.blue,
        ),
        onPressed: selectHandler,
        child:  Text(answerText!),
        
      ),
    );
  }
}

Upvotes: 1

Views: 175

Answers (2)

haitham
haitham

Reputation: 87

just change VoidCallback to void Function(int).

Upvotes: 1

lepsch
lepsch

Reputation: 10389

You should change the following code in the Quiz class:

final VoidCallback answerQuestion;

to:

final Function(int) answerQuestion;

Upvotes: 0

Related Questions