2

I'm trying to create a stateless widget like this:

import 'package:flutter/material.dart';

class Answers extends StatelessWidget {
  final Function nextQuestion;

  Answers(this.nextQuestion);

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: nextQuestion,
      child: Text("Answer"),    
    );
  }
}

However, an error appears in the onPressed argument: "The argument type 'Function' can't be assigned to the parameter type 'void Function()?'". How can I solve it?

enzo
  • 9,861
  • 3
  • 15
  • 38
  • 1
    Does this answer your question? [error: The argument type 'Future' can't be assigned to the parameter type 'void Function()'](https://stackoverflow.com/questions/57761489/error-the-argument-type-future-cant-be-assigned-to-the-parameter-type-void) – padaleiana Jun 27 '21 at 16:15

3 Answers3

7

Just change the type of nextQuestion to void Function():

final void Function() nextQuestion;
enzo
  • 9,861
  • 3
  • 15
  • 38
0

changing

  final Function nextQuestion;

to

final VoidCallback nextQuestion;

This solved my issue.

Shishir Rijal
  • 133
  • 1
  • 5
0

Change type of nextQuestion to VoidCallback which can be used if you have a function which returning no data with no arguments. change your datatype from Function to VoidCallback.

final VoidCallback nextQuestion;

       or you can put anonymous function in front of onPressed (){}

onPressed: ()=>nextQuestion,

asad jani
  • 51
  • 6