The argument type 'Function' can't be assigned to the parameter type 'void Function()?' after null safety

60,532

Solution 1

Change your code to accept a VoidCallback instead of Function for the onPressed.
By the way VoidCallback is just shorthand for void Function() so you could also define it as final void Function() onPressed;

Updated code:

class DrawerItem extends StatelessWidget {
    
      final String text;
      final VoidCallback onPressed;
    
      const DrawerItem({Key key, this.text, this.onPressed}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return FlatButton(
          child: Text(
            text,
            style: TextStyle(
              fontWeight: FontWeight.w600,
              fontSize: 18.0,
            ),
          ),
          onPressed: onPressed,
        );
      }
    }

Solution 2

Dart 2.12 (Null safety):

Instead of

final Function? onPressed; // Bad

use

final void Function()? onPressed; // Good
final VoidCallback? onPressed; // Good

Solution 3

Well that's because onPressed inside FlatButton is not a normal function its VoidCallBack Function. You can try something like this:

final VoidCallBack onPressed;

While, you are passing a normal function into a VoidCallBack

Follow the official doc here

enter image description here

Updated Code:

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomeScreen(),
    );
  }
}

class HomeScreen extends StatelessWidget {
  _myFunction() => print("Being pressed!");

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            DrawerItem(
              text: "Hello Jee",
              onPressed: _myFunction,
            ),
          ],
        ),
      ),
    );
  }
}

class DrawerItem extends StatelessWidget {
  final String text;
  final Function onPressed;

  const DrawerItem({Key key, this.text, this.onPressed}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return FlatButton(
      child: Text(
        text,
        style: TextStyle(
          fontWeight: FontWeight.w600,
          fontSize: 18.0,
        ),
      ),
      onPressed: onPressed,
    );
  }
}

Solution 4

Put the parentheses () after the keyword Function when defining the function.

import 'package:flutter/material.dart';

class Answer extends StatelessWidget {
  final Function() onPressed; //parenthesis () on function
  final String name;
  const Answer(this.name, this.functionMessage, {Key? key}) : super(key: 
key);

  @override
  Widget build(BuildContext context) {
    return Container(
        margin: const EdgeInsets.only(top: 10),
        width: double.infinity,
        child: ElevatedButton(
           style: ElevatedButton.styleFrom(
           primary: Colors.black, // background
           onPrimary: Colors.white, // foreground
          ),
          onPressed: onPressed,
          child: Text(name),
        ));
  }
}

Solution 5

use:

VoidCallback? _onPressed

instead of:

VoidCallback _onPressed

it's work for me!

Share:
60,532
dosytres
Author by

dosytres

Updated on July 08, 2022

Comments

  • dosytres
    dosytres almost 2 years

    I want to achieve to make a drawer with different items on it, so I am creating a separate file for the DrawerItems and the with the constructor, pass the data to the main file. But I get the following error on the onPressed function:

    "The argument type 'Function' can't be assigned to the parameter type 'void Function()'"
    
    class DrawerItem extends StatelessWidget {
        
          final String text;
          final Function onPressed;
        
          const DrawerItem({Key key, this.text, this.onPressed}) : super(key: key);
        
          @override
          Widget build(BuildContext context) {
            return FlatButton(
              child: Text(
                text,
                style: TextStyle(
                  fontWeight: FontWeight.w600,
                  fontSize: 18.0,
                ),
              ),
              onPressed: onPressed,
            );
          }
        }
    

    Anyone knows why?

  • dosytres
    dosytres over 3 years
    No, its not that the problem is still using final VoidCallBack onPressed; But thank you.
  • Hamza
    Hamza over 3 years
    Your code is working fine for me, what's your Flutter version? Or can you show me which function are you passing as parameter to DrawerItem
  • dosytres
    dosytres over 3 years
    my flutter version is 1.22.o, I know its old, but I have to use this one because i have an error with java and it wont work with an updated one.
  • Hamza
    Hamza over 3 years
    Actually your code is fine here, can you show me which type of function you are passing as a parameter?
  • dosytres
    dosytres over 3 years
    This is the full code, then iI want to be able to pass it to another file with the constructor
  • Askani
    Askani about 3 years
    final Function()? onPressed; worked for me , thank you :)
  • Hitesh Danidhariya
    Hitesh Danidhariya about 3 years
    Is there any solution if I want a function with argument in it.For example Textfield function "onSubmitted", because it gives me the error as "The argument type 'void Function()?' can't be assigned to the parameter type 'void Function(String)?".Any solution for this error?
  • Pieter van Loon
    Pieter van Loon about 3 years
    @HiteshDanidhariya yes you can simply add it in the brackets, i.e. void Function(String myString)
  • vietstone
    vietstone almost 3 years
    Can you explain the logic behind it? Or point me some link about this? Thank you.
  • CopsOnRoad
    CopsOnRoad almost 3 years
    @vietstone A Function can be anything, like Function(), Function(int), etc, which is why with Dart null safety, you should explicitly tell what that Function is.
  • vietstone
    vietstone almost 3 years
    @CopsOnRoad Thank you for your point. But I wonder that, as you say about Function() and Function(int), I think this is a kind of Polymorphism, not really Null Safety :D
  • CopsOnRoad
    CopsOnRoad almost 3 years
    @vietstone Polymorphism is a different thing. Function onPressed can be invoked by onPressed(), onPressed(1), onPressed(1, true) so, you may run into runtime error but Function() onPressed can only be invoked by onPressed(). This thing is not particular related to null safety but Dart 2.12.
  • vietstone
    vietstone almost 3 years
    @CopsOnRoad yes, just curious about the name of this technique specifically
  • sina
    sina about 2 years
    short & effective answer... thanks :)