Sending an Image as an Argument for a Flutter Platform Channel Method

1.9k Views Asked by At

I am building a Flutter Application, and for one of the API's I am using, it does not have Flutter support, only Android and iOS. My solution to this was to use Platform Channels, but how would I pass an Image as an argument?

To explain a little further, I am picking an image from the gallery with ImagePicker().getImage in the dart file, and I want to send the image selected to the method in the Kotlin file that will do something with the image on its end and return a string.

After looking at the docs, I was able to make a channel like this:

static const platform = const MethodChannel('app.dev/channel'); 
final string result = await platform.invokeMethod('returnStringfromImage');

And in the Kotlin file:

 private val CHANNEL = "app.dev/channel"

    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
            // Note: this method is invoked on the main thread.
            call, result ->
            if (call.method == "returnStringfromImage") {
                val return = returnStringfromImage(call.arguments)
                
            }

            else {
                result.notImplemented()
            }
        }

    }

How would I send the image over, and pass it as an argument for the returnStringfromImage() method? Thank you!

1

There are 1 best solutions below

0
On

You need to convert the picked image to any of these supported types, most probably as bytes (Uint8list) to send over to the platform side.

ImagePicker would return a File, so You can call readAsBytes on it to get Uint8list. Then you can pass it via arguments of invokeMethod.