title: Flutter Platform Channels - Invoke channel method on android, hangs the ui
body: <p>I'm trying to use Tesseract in flutter using the following package <a href="https://github.com/arrrrny/tesseract_ocr" rel="nofollow noreferrer">https://github.com/arrrrny/tesseract_ocr</a></p>
  
  <p>I've download the app and run in.</p>
  
  <p>The problem is that the <code>extractText</code> hangs the UI.</p>
  
  <p>Looking at the Java code:</p>
  
  <pre><code>  Thread t = new Thread(new Runnable() {
      public void run() {
        baseApi.setImage(tempFile);
        recognizedText[0] = baseApi.getUTF8Text();
        baseApi.end();
      }
    });
    t.start();
    try { t.join(); } catch (InterruptedException e) { e.printStackTrace(); }
    result.success(recognizedText[0]);
  </code></pre>
  
  <p>I can see that it is running on a new thread, so I expect it not to hang the app, but it still does.</p>
  
  <p>I found this example:</p>
  
  <pre><code>            new Handler(Looper.getMainLooper()).post(new Runnable() {
                  @Override
                  public void run() {
                      // Call the desired channel message here.
                      baseApi.setImage(tempFile);
                      recognizedText[0] = baseApi.getHOCRText(0);
                      baseApi.end();
                      result.success(recognizedText[0]);
  
                  }
              });
  </code></pre>
  
  <p>from <a href="https://flutter.dev/docs/development/platform-integration/platform-channels#channels-and-platform-threading" rel="nofollow noreferrer">https://flutter.dev/docs/development/platform-integration/platform-channels#channels-and-platform-threading</a></p>
  
  <p>but it also hangs the UI.</p>
  
  <p>The docs also say</p>
  
  <pre><code>**Channels and Platform Threading**
  Invoke all channel methods on the platform’s main thread when writing code on the platform side.
  </code></pre>
  
  <p>Can someone clarify this sentence?</p>
  
  <p>According to <code>Richard Heap</code> answer, I tried to call a method from native to dart, passing the result:</p>
  
  <p>Dart side:</p>
  
  <pre><code>_channel.setMethodCallHandler((call) {
    print(call);
    switch (call.method) {
      case "extractTextResult":
        final String result = call.arguments;
        print(result);
    }
    var t;
    return t;
  });
  </code></pre>
  
  <p>Java side:</p>
  
  <p>channel.invokeMethod("extractTextResult","hello");</p>
  
  <p>if I call this method from the main thread, this works fine,  but then the thread is blocking.</p>
  
  <p>If I do</p>
  
  <pre><code>            Thread t = new Thread(new Runnable() {
                  public void run() {
                      channel.invokeMethod("extractTextResult","test1231231");
  
                  }
              });
              t.start();
  
              result.success("tst"); // return immediately
  </code></pre>
  
  <p>Then the app crashes with the following message:</p>
  
  <p><a href="https://i.stack.imgur.com/5QTNA.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5QTNA.jpg" alt="enter image description here"></a></p>
  
  <p>I also tried:</p>
  
  <pre><code>           Thread t = new Thread(new Runnable() {
                  public void run() {
                      new Handler(Looper.getMainLooper()).post(new Runnable() {
                          @Override
                          public void run() {
                              // Call the desired channel message here.
                              baseApi.setImage(tempFile);
                              recognizedText[0] = baseApi.getHOCRText(0);
                              baseApi.end();
                              result.success(recognizedText[0]);
                              //                                channel.invokeMethod("extractTextResult", "test1231231");
                          }
                      });
  
                  }
              });
              t.start();
  
              result.success("tst");
  </code></pre>
  
  <p>which is what I understand that <code>Richard Heap</code> last comment meant, but It still hangs the ui.</p>