Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter onPressed Function

Error in onPressed
Trying to pass onPressed Function but getting Error.

Error – The argument type ‘Function’ can’t be assigned to the parameter type ‘void Function()?’. enter image description here

class CircleButton extends StatelessWidget {
  CircleButton({required this.icon, required this.onPressed});
  final IconData icon;
  final Function onPressed;                 [![Error][1]][1]//Here
  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      child: Icon(icon),
      onPressed: onPressed,
      style: ElevatedButton.styleFrom(
        elevation: 6.0,
        primary: Color(0xFF4C4F5E),
        shape: CircleBorder(),
        padding: EdgeInsets.all(15),
      ),);
  }}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The Function type is actually a dynamic Function type.

The ElevatedButton expects a void Function to its onPressed method, which there is actually a type that is a typedef for it: VoidCallback

So, replace your Function onPressed with VoidCallback onPressed.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading