简体   繁体   中英

What is the equivalent of this kotlin code in dart?

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.

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

Turns out it's pretty simple. Silly me.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM