Repository: flutter
Issue: InvokeMethod issue on Android side, MethodChannel and EventChannel must be executed on the main thread 
Labels: platform-android,engine,d: api docs,P3,a: plugins,team-android,triaged-android

<!-- Thank you for using Flutter!

     If you are looking for support, please check out our documentation
     or consider asking a question on Stack Overflow:
      * https://flutter.dev/
      * https://api.flutter.dev/
      * https://stackoverflow.com/questions/tagged/flutter?sort=frequent

     If you have found a bug or if our documentation doesn't have an answer
     to what you're looking for, then fill our the template below. Please read
     our guide to filing a bug first: https://flutter.dev/docs/resources/bug-reports
-->

## Steps to Reproduce

<!-- Please tell us exactly how to reproduce the problem you are running into. -->

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

<!--
      Include the full logs of the commands you are running between the lines
      with the backticks below. If you are running any "flutter" commands,
      please include the output of running them with "--verbose"; for example,
      the output of running "flutter --verbose create foo".
-->
Setting methodCallHandler on Flutter:

  _channel.setMethodCallHandler(_checkPassword);

![image](https://user-images.githubusercontent.com/43069274/61110636-fcc7bf00-a487-11e9-8e48-bd5e139c2721.png)


Invoking method on Android:

![image](https://user-images.githubusercontent.com/43069274/61108988-0c450900-a484-11e9-8557-cdde5e0b6aa9.png)

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!
```
```