Home > database >  Flutter callback / passing function as a parameter problem
Flutter callback / passing function as a parameter problem

Time:02-12

I'm trying to pass a function as a parameter. My code runs but the function doesn't fire.

I have a Holiday class

  final VoidCallback callback;
  Holiday(this.holidayStart, this.holidayEnd, this.holidayDuration, this.callback);

Inside the Holiday class I have a function to build a widget with a button. here I use...

   onPressed: () {
       callback;
   },

In my main.dart file when I create a new instance of Holiday I pass a function like this....

holidays.add(Holiday(startSelected, endSelected, stay, () { print('pressed'); }))

Everything compiles fine but pressed is never printed. Any ideas what I'm doing wrong here?

Thanks in advance.

CodePudding user response:

In your case, you passed function that does nothing because you put callback but don't call it (via () at the end or .call()). You should write onPressed: callback, or onPressed: () { callback(); },

  • Related