omidh
omidh

Reputation: 2822

Dart: Declare function return type when function is an input

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?

Upvotes: 15

Views: 11438

Answers (1)

Kevin Moore
Kevin Moore

Reputation: 6161

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

Upvotes: 30

Related Questions