How to return value from VoidCallback function


I have a custom widget which has a button. I want to create VoidCallback faction to return data from it.

onDayPressed: (DateTime date, List<Event> events){
   // I want to return date time to original class when the user changes the date 

i try to do this but doesn’t work

onDayPressed: (DateTime date, List<Event> events) => widget.onDayChanged,

// on original class

CustomCalender(onDayChanged: (){print("HI");}),

I’m using this flutter package



If you need to pass data from child to parent you must use Function(T), since a VoidCallback do not support passing data. See the code example below:

// Try adapting this code to work with your code, the principle should be the same.
class MinimalExample extends StatelessWidget {
  Widget build(BuildContext context) {
    return Scaffold(
      // Notice the variable being passed in the function.
      body: ReturnValueToParent( myNumber: (int) => print(int),),

// Specify a Function(DateTime) in your case and adapt it to your problem.
class ReturnValueToParent extends StatelessWidget {
  final Function(int) myNumber;
  const ReturnValueToParent({Key key, this.myNumber}) : super(key: key);

  Widget build(BuildContext context) {
    return Center(
      child: RaisedButton(
        // Remember the parameter
        onPressed: () => myNumber(5),

Answered By – Tor-Martin Holen

Answer Checked By – Timothy Miller (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.