Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart assign a function/method to variable after declaration

Tags:

flutter

dart

I want to assign a function, with parameters, to an already declared variable, So I be able to execute it later.

Something like that:

void main() {
  Function p;
  p = print('1'); // should not execute;
  p;
}

How do I do that? Is it possible?

like image 292
genericUser Avatar asked Jun 15 '26 18:06

genericUser


1 Answers

You can do it like

void main() {
  late Function p;
  p = () {
    print('1');
  };
  p(); // it will print 1
}

like image 109
Yeasin Sheikh Avatar answered Jun 17 '26 07:06

Yeasin Sheikh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!