4

Creating a platform channel on Android for File upload using AWS SDK. Now I want to wait for the upload to complete in the background and return the status of the result.

MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
            if (call.method == "uploadToAWS") {
                new DoUpload().execute();

                // how to await here ?
                result.success(true);
            } else {
                result.notImplemented()
            }
        }
krishnakumarcn
  • 3,959
  • 6
  • 39
  • 69
  • simply call `result.success(true);` when you are done with uploading – pskink Feb 19 '20 at 06:56
  • can I call the result.success(true) from inside a delegate function? – krishnakumarcn Feb 19 '20 at 06:58
  • 1
    you can call it from everywhere, the only restriction is described [here](https://api.flutter.dev/javadoc/io/flutter/plugin/common/MethodChannel.Result.html) - read the second paragraph – pskink Feb 19 '20 at 07:01
  • 1
    the easiest way is something like this: `new DoUpload(result).execute();` - so you can access `result` inside your `DoUpload` class – pskink Feb 19 '20 at 07:13

1 Answers1

6
  1. Instead of waiting there, remove "result.success(true);" statement from there and simply assign "result" object to your own object of type "MethodChannel.Result".

  2. Then use it to execute its "success", "error" or "notImplemented" method from anywhere as shown in below example.

    public class Activity extends FlutterActivity {
    
      private MethodChannel.Result myResult;
    
      new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
            .setMethodCallHandler(
                    (call, result) -> {
                        myResult = result;
                    }
            );
    
      private void myCustomMethod {
    
        myResult.success(yourData);
    
      }
    
    }
    

Now you can use these myResult.success(), myResult.error() methods from anywhere within class.

user3930098
  • 395
  • 7
  • 14