Skip to content Skip to sidebar Skip to footer

Java.lang.runtimeexception: Methods Marked With @uithread Must Be Executed On The Main Thread. Current Thread: Defaultdispatcher-worker-2

I'm quite new about flutter and kotlin. I'm doing flutter version up(from 1.0.0 to 1.7.8+hotfix4) recently. After I upgrade kotlin version to 1.3.10, my flutter app came into cras

Solution 1:

First of all you should avoid using global scope, it is equivalent to creating a thread in java, you can read more here about this.

Second thing is that you should know that you can modify UI only on the Main thread. Most probably methodResult?.success(it) does some UI updates, so if you want a fast fix :

GlobalScope.launch {
              async {
                var ls = LoginManager.getInstance().loginServicevar response = ls.authRequest<AuthResponse<TokenResult>>(AUTH_PROVIDER_NAME)
                return@async response.getToken()
              }.await().let {
                withContext(Dispatchers.Main) {
                  methodResult?.success(it)
                }
              }
            }

When you call GlobalScope.launch it implies that you launch the coroutine as Dispatchers.Default witch means it will create a worker thread that is usually used for intensive computation, if you use it for network requests a better solution would be to launch it with dispatchers IO:

// instead of GlobalScope.launch CoroutineScope(Dispatchers.IO).launch {
  // your code goes here
}

Post a Comment for "Java.lang.runtimeexception: Methods Marked With @uithread Must Be Executed On The Main Thread. Current Thread: Defaultdispatcher-worker-2"