1

So, we have MethodChannel that's covered in the Writing custom platform-specific code document section that sends a single request to Javaland and returns a single response, but what I'd like to do is set up a literal pubsub channel between services running on Android & my flutter app, this would allow an alternative approach to my earlier question about passing closures as arguments to Java-side since it would allow the function

Future<Result> = doPayment(
  amount: 100.00, 
  currency: 'USD', 
  onPasscodeEntry: () => _renderInputBox(), 
  onValidation: () => _doValidation()
);

to be implemented by doing something like:

Future<Result> doPayment({int amount, String currency, Function onPasscodeEntry, Function onValidation}) {
    Subscription sub;
    try {
        sub = subscribe(channel: 'myapp/doPayment/$paymentUid')
        sub.send("initialize", {"amount": amount, "currency": currency});

        sub.receive("onPasscodeEntry").then((payload) => onPasscodeEntry(payload));
        sub.receive("onValidation").then((payload) => onValidation(payload));

        return sub.receive("result")
    finally {
        sub?.send("deinitialize");
        sub?.end()
        return null;
    }
}

You get the idea :-)

It seems like BasicMessageChannel could be my friend, but there's no usage example, and there's no samples on how to hook into it on the platform-side (aka Javaland), and I'd expect a receive function too - something that waits on something happening in Javaland and pushing a notification to Flutter-side,

a simple example of this would be if the MethodChannel usage example had the ability to push battery status updates into flutter, and not just fetch them from flutter on demand.

It's not specifically necessary for the above example, but I'd like to set up a 2-way communication channel with the platform - for what I'm doing I just need to know at this point that this door is not closed to me if I go for Flutter.

I'm looking for the right classes/libraries to use flutter-side and java-side and code samples since that part is not really very well covered in the docs.

1
  • You can use it like this: val channel = BasicMessageChannel(activity.flutterView, appChannel, StringCodec.INSTANCE); channel.send(message); where activity is instance of FlutterActivity, appChannel is app channel name string, and message is your string. Jul 3, 2019 at 14:40

1 Answer 1

0

It seems this is currently possible using a combination of MethodChannel & EventChannel - one messages from Flutter to platform, one from platform to Flutter (once Flutter-side subscribes) - as demonstrated in the example on Github (there's also a separate example for swift).

Technically this means a pub-sub can be implemented, it's just not nice to do it, I wrote an issue about it to Flutter's team - suggesting a Stream-based API instead (and then you can leverage rxdart, rxjava, rxswift & others), so we'll see how it goes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.