On Android side, I need to get a data that will be returned by Flutter side MethodChnnel.
As we know, on Flutter side, we can easily user Dart async/await() the result from Native side. But on Android side, it seems that can't use runBlocking or similar ways to wait for the callback to finish and get the returned data. Using runblocking will cause a UI frozen .
Is that possible to acheive someting like this? Note: runBlcking in the following code doesn't work and causes a frozen
fun getFlutterData(): String? {
var res: String? = null
runBlocking {
res = suspendCoroutine {
channel!!.invokeMethod(flutterMethod, params, object : MethodChannel.Result {
override fun success(result: Any?) {
println("callFlutterMethod success: $result")
** it.resume(res)**
}
override fun error(errorCode: String, errorMessage: String?, errorDetails: Any?) {
println("callFlutterMethod error: $errorCode, $errorMessage,$errorDetails")
}
override fun notImplemented() {
}
})
}
}
** return res **// get the result and assign to other variable
}
You should use kotlin coroutines.
When you use
runBlocking, you freeze current thread, in you case main (ui) thread. Instead you should wait for callback in background.and then you can call the function for example
I recommend you to learn more about kotlin coroutines and coroutine scope.
Note, that you must run
methodChannel.invokeMethodon main thread.