Zach Smith
Zach Smith

Reputation: 5704

Calling multiple callbacks with Dart/Flutter

I'm using an utility class that I wrote which shows an alert dialog with various actions. Per each action, I can utilize the onPressed method. Previously I've been using it to simply close the alert dialog, such as:

onPressed: () => Navigator.of(context).pop(true)

This works great, until I want to pass an additional callback to be fired before I trigger the Navigator. When I'm passing in a callback into this class, I'm trying something like:

onPressed: () {
  this.cancelCallback;
  Navigator.of(context).pop(false);
},

Where this.cancelCallback is the VoidCallback passed into this widget class. However, this is apparently the wrong syntax and Flutter is just passing over that and going straight to the Navigator logic.

The basic reasoning here is I'd want to pass in any callback logic to be fired prior to a cancel/accept event happening in an alert dialog. I can't figure out how to pass in a callback and fire this Navigator callback at the same time.

Upvotes: 2

Views: 1355

Answers (1)

Yaya
Yaya

Reputation: 4848

for calling functions in flutter you should add () after function name function(); and not function; in js , () indicates a call to the function.
Without ()
you've got just a reference to the function, in dart it has different
so

onPressed: () {
  this.cancelCallback();
  Navigator.of(context).pop(false);
},

Upvotes: 2

Related Questions