2

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.

1 Answer 1

2

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(); },

1
  • Ah thank you! knew it would be something silly. I changed it to your first suggestion.
    – Lee Casey
    Commented Feb 12, 2022 at 9:21

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.