3

I'm playing with PlatformChannel and MethodChannel in Flutter. Doing some basic operations... working on a opencv porting for flutter... but i can't figure out how to solve this!


There are no problems invoking simple methods, that returns objects of basic Types, like this:

Android:

@Override
  public void onMethodCall(final MethodCall call, final Result result) {
    switch (call.method) {
      case "getVersionString":
        result.success(org.opencv.core.Core.getVersionString());
        break;
      default:
        result.notImplemented();
    }
  }

Dart:

Future<String> get versionString async {
  return await _channel.invokeMethod('getVersionString') as String;
}


But what if i have some custom object? Something like:
Android:

  @Override
  public void onMethodCall(final MethodCall call, final Result result) {
    switch (call.method) {
      case "getVersionString":
        result.success(org.opencv.core.Core.getVersionString());
        break;
      case "Scalar":
        result.success(org.opencv.core.Core.Scalar());
        break;
      default:
        result.notImplemented();
    }
  }
}

Dart:

class Scalar {
  static const MethodChannel _channel = MethodChannel('flutter_opencv');

  // WHAT TO DO HERE??????
}  

How to implement the Scalar dart class? I need to rewrite the class starting from original sources or what?


Thanks for the attenction.

Roberto Manfreda
  • 2,345
  • 3
  • 25
  • 39

1 Answers1

8

The StandardMessageCodec can only send certain types across the boundary. Also, you can't send a Java class across the boundary - what would it turn into at the Dart end?

There are things that you can do:

  1. Serialize the Java object to JSON, transfer the string, then deserialize as a Dart object

  2. Put the Java object's fields into a HashMap (key is a string, value is a supported type), transfer it (will appear as a Map<String, dynamic>) and access the fields.

  3. Write your own codec (or subclass the standard codec) where you end up basically doing (2) but with less overhead of the string field names. (This is tricky...)

Richard Heap
  • 48,344
  • 9
  • 130
  • 112
  • Yes i had already read the documentation regarding `StandardMessageCodec`... i'm trying going on following your 3d point! Thanks for your effort. I imagined that was possible using JSON too... finally it's necessary wrap the class into dart code, there is no way to excape! Thanks again :) – Roberto Manfreda Jul 01 '19 at 17:00
  • I'm trying using json and i can see that is better!! :) – Roberto Manfreda Jul 03 '19 at 10:47
  • 1
    Take a look at this answer for the (2) technique. For what it's worth, that's the method we normally use. https://stackoverflow.com/questions/56852851/how-do-i-best-give-multiple-arguments-with-the-java-version-of-flutters-methodch/56855583#56855583 Regarding (3), you think it would be easy to extend `StandardMessageCodec` but some of things you need to access are private, so you end up copying bits of boilerplate. I've done it to add support for float[] <-> Float32List - and it was a pain. – Richard Heap Jul 03 '19 at 10:52