Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart: Declare function return type when function is an input

Tags:

dart

So In my class constructor I want to accept a function as input so:

class foo() {
  final Function inpFunc;
  foo({this.inpFunc});
}

I want to only accept functions that return int but final Function<int> inpFunc is not valid

What should I do?

like image 929
omidh Avatar asked Jan 22 '26 18:01

omidh


1 Answers

You can add return type and parameter details to be more specific.

int Function() for instance.

class Foo {
  final int Function() inpFunc;
  Foo(this.inpFunc);
}

main() {
  var foo = Foo(() => 42);

  print(foo.inpFunc());
}
like image 65
Kevin Moore Avatar answered Jan 25 '26 21:01

Kevin Moore



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!