[SOLVED] The argument type 'Function' can't be assigned to the parameter type 'void Function()?' Dart

Issue

import 'package:flutter/material.dart';

class Answer extends StatelessWidget {
  final Function selectHandler;

  Answer(this.selectHandler);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      color: Colors.pink,
      child: ElevatedButton(
        child: Text('Answer 1'),
        onPressed: selectHandler,
      ),
    );
  }
}


[screenshot of vscode][1]

I got this code that I use from a Dart (flutter) YouTube course (that is from 2019) and i get an error on the last "selectHandler" (after onPressed) -> "The argument type ‘Function’ can’t be assigned to the parameter type ‘void Function()?’" I don’t really understand because it is working well in the video.
I am pretty sure that the course is too old but if you can help me that could be cool. Thanks ! (PS: my flutter doctor is good)

Solution

Replace

final Function selectHandler;

with

final void Function() selectHandler;

see this ref doc

Answered By – navylover

Answer Checked By – Timothy Miller (BugsFixing Admin)

Leave a Reply

Your email address will not be published.