Home > Software engineering >  What is the equivalent of this kotlin code in dart?
What is the equivalent of this kotlin code in dart?

Time:05-03

In kotlin I was able to create a method like this.

fun add(a:int, b:int, output: (int) -> Unit {
  val sum = a   b
  output.invoke(sum)
}

Then I could call this method like this:

add(4,5){ sum ->
  print(sum) //9
}

Is this possible in dart? I have looked at a lot of sources but havn't been able to find any help. Any help is greatly appreciated.

CodePudding user response:

That Kotlin syntax looks fishy, there must be missing brackets. I don't know Kotlin, but I think you want this:

void add(int a, int b, Function(int) output) {
  final sum = a   b;
  output.call(sum);
}

void main() {
  add(4,5, (sum) => print(sum));
}

CodePudding user response:

Turns out it's pretty simple. Silly me.

void add(int a, int b, void c(int)) {
  return c.call(5);
}
  • Related