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 callback / passing function as a parameter problem

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…

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

   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.

>Solution :

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

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