How do you determine if a variable is "callable" in a dart?

I am doing a little experiment in a dart, and I could not find a way to determine if the variable is “callable”, without explicitly checking for each type (String, int, bool, ect) and assuming that it was called if it is none of this . I also experimented with try / catch, which just seems wrong to me.

What is the right way, or at least the best way to make this determination?

Here is an example that I did to show what I'm trying to execute: https://gist.github.com/digitalfiz/3f431dc07ca761389062

+4
source share
1 answer
class Callable{
  call() => 42;
}
void main() {
  var foo = ()=>42;
  var bar = new Callable();
  var baz = 42;
  bool isCallable(v) => v is Function;
  print(isCallable(foo)); //true
  print(isCallable(bar)); //true
  print(isCallable(baz)); //false
}
+6
source

Source: https://habr.com/ru/post/1543305/


All Articles