'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.
Solution 1:[1]
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));
}
Solution 2:[2]
Turns out it's pretty simple. Silly me.
void add(int a, int b, void c(int)) {
return c.call(5);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | nvoigt |
| Solution 2 | Danish Ajaib |
