Skip to content

InvokeMethod issue on Android side, MethodChannel and EventChannel must be executed on the main thread  #36051

@MadScientist996

Description

@MadScientist996

Steps to Reproduce

Currently developing an App in flutter which is communicating with Android through EventChannels and MethodChannels,
got some problem since I updated flutter from 1.5.4 to 1.7.8. So basically my workflow follows this steps.

  1. On android side I am waiting for detection of sensor
  2. On detection i read it and invoke a method from Flutter
  3. Flutter generates a string based on argument, which he returns over method channel
  4. When i get the String back on Android side, I use that String and run other methods
  5. I send a variable on EventStream sink back to flutter
  6. Flutter listens to stream and displays the content

And there are a couple of issues, your invoke method needs to run on MainThread, same goes for pushing variables on EventSink. Is this a bug in the new version or you just want us to do with a single thread? Second issue i found was even in the previous version, I simply don't know how and didn't find anything on the internet or your docs about awaiting results of invokeMethod on Android side, in flutter you can simply create a Future method and await it, but in Kotlin i don't know how (Currently time racing with Thread.Sleep(50ms)). Any suggestions? Am i doing something wrong?
I am doing all of this on MainActivity, is this the source of problem?
I tried to wrap it inside runOnUiThread, but it freezes my UI for the doing, would like to work it as async.
What are your words on that, what direction will you go next?

Logs

Setting methodCallHandler on Flutter:

_channel.setMethodCallHandler(_checkPassword);

image

Invoking method on Android:

image

Log error after update (Same for Invoke method and EventChannel):

W/Binder: Caught a RuntimeException from the binder stub implementation.
    java.lang.RuntimeException: Methods marked with @UiThread must be executed on the main thread. Current thread: Binder:10473_3
        at io.flutter.embedding.engine.FlutterJNI.ensureRunningOnMainThread(FlutterJNI.java:794)
        at io.flutter.embedding.engine.FlutterJNI.dispatchPlatformMessage(FlutterJNI.java:684)
        at io.flutter.embedding.engine.dart.DartMessenger.send(DartMessenger.java:80)
        at io.flutter.embedding.engine.dart.DartExecutor.send(DartExecutor.java:189)
        at io.flutter.view.FlutterNativeView.send(FlutterNativeView.java:155)
        at io.flutter.view.FlutterView.send(FlutterView.java:750)
        at io.flutter.view.FlutterView.send(FlutterView.java:740)
        at io.flutter.plugin.common.EventChannel$IncomingStreamRequestHandler$EventSinkImplementation.error(EventChannel.java:235)
        at com.tracklegit.mobile.MainActivity.onTagDiscovered(MainActivity.kt:145)
        at android.nfc.NfcActivityManager.onTagDiscovered(NfcActivityManager.java:602)
        at android.nfc.IAppCallback$Stub.onTransact(IAppCallback.java:83)
        at android.os.Binder.execTransact(Binder.java:739)

<!-- If possible, paste the output of running `flutter doctor -v` here. -->
[√] Flutter (Channel stable, v1.7.8+hotfix.3, on Microsoft Windows [Version 10.0.17763.615], locale sl-SI)
    • Flutter version 1.7.8+hotfix.3 at D:\Games\flutter
    • Framework revision b712a172f9 (2 days ago), 2019-07-09 13:14:38 -0700
    • Engine revision 54ad777fd2
    • Dart version 2.4.0

[√] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    • Android SDK at C:\Users\blazj\AppData\Local\Android\sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-28, build-tools 28.0.3
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)
    • All Android licenses accepted.

[√] Android Studio (version 3.4)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin version 37.0.1
    • Dart plugin version 183.6270
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)

[√] VS Code (version 1.36.1)
    • VS Code at C:\Users\blazj\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.2.0

[√] Connected device (1 available)
    • SM G950F • 988a5c344849314e57 • android-arm64 • Android 9 (API 28)

• No issues found!

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projecta: pluginsSupport for writing, building, and running plugin packagesd: api docsIssues with https://api.flutter.dev/engineflutter/engine related. See also e: labels.platform-androidAndroid applications specificallyteam-androidOwned by Android platform teamtriaged-androidTriaged by Android platform team

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions