Home > Software engineering >  lib/answer.dart:16:20: Error: The argument type 'Function' can't be assigned to the p
lib/answer.dart:16:20: Error: The argument type 'Function' can't be assigned to the p

Time:12-04

This is the main.dart file:

    import 'package:flutter/material.dart';
    import './question.dart';
    import './answer.dart';
    import 'package:path/path.dart';
    
    void main() {
      runApp(MyFirstApp());
    }
    
    class MyFirstApp extends StatefulWidget {
      @override
      State<StatefulWidget> createState() {
        // TODO: implement createState
        return _MyFirstAppState();
      }
    }
    
    class _MyFirstAppState extends State<MyFirstApp> {
      var _questionIndex = 0;
      void _ansButtonPress(context) {
        setState(() {
          _questionIndex = _questionIndex   1;
        });
      }
    
      var questions = ['favorate color ', 'favorate animal', 'where is this'];
      var answers = ['Option 1', 'Option 2', 'Option 3'];
    
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(
              title: Text('My first app '),
            ),
            body: Column(
              children: [
                Question(
                  questions[_questionIndex],
                ),
                Answer(_ansButtonPress),
                Answer(_ansButtonPress),
                Answer(_ansButtonPress),
              ],
            ),
          ),
        );
      }
    }
    

This is answwers.dart file:

    import 'package:flutter/material.dart';
    
    class Answer extends StatelessWidget {
      final Function selectHandler;
    
      Answer(this.selectHandler);
      // const Answer({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Container(
          width: double.infinity,
          child: RaisedButton(
            color: Colors.blue,
            child: Text('Answer 1'),
            onPressed: selectHandler,
          ),
        );
      }
    }`enter code here`
    [enter image description here][1]

This is questions.dart:

    import 'package:flutter/material.dart';
    
    class Answer extends StatelessWidget {
      final Function selectHandler;
    
      Answer(this.selectHandler);
      // const Answer({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Container(
          width: double.infinity,
          child: RaisedButton(
            color: Colors.blue,
            child: Text('Answer 1'),
            onPressed: selectHandler,
          ),
        );
      }
    }
   
    

image of the error

I am working on a quiz app by watching a tutorial, I am just a beginner now facing an error. I can't fix it, I added the code and error with this.

lib/answer.dart:16:20: Error: The argument type 'Function' can't be assigned to the parameter type 'void Function()?'.

  • 'Function' is from 'dart:core'. onPressed: selectHandler,

CodePudding user response:

The onPressed property of the button in question is a VoidCallback?, so for ease of use, make your property a VoidCallback (nullable or not is up to you), too:

final Function selectHandler;

becomes

final VoidCallback selectHandler;

Then make sure the method you pass to this property actually is of this signature:

void _ansButtonPress(context) {

becomes

void _ansButtonPress() {

I don't know what that "context" was supposed to do anyway, it wasn't used.

Now it should work.

  • Related