0phelia
6/18/2018 - 11:39 AM

Flutter channels



EventChannel(getFlutterView(), 
CHANNEL_PLAYBACK_EVENTS, 
JSONMethodCodec.INSTANCE)
.setStreamHandler(
       object : EventChannel.StreamHandler {
           override fun onListen(args: Any?, 
               events: EventChannel.EventSink) {
               playbackEventSink = events
           }

           override fun onCancel(args: Any?) {
               // free resources 
           }
       })
static const playbackEventChannel = const EventChannel('flutterify/playback', 
                         const JSONMethodCodec());




playbackEventChannel.receiveBroadcastStream()
                                       .listen((meta) {
                                           metadataCallback(meta);
                                        });






















  static const myMethodChannel =
    const MethodChannel('methodChannelName');
  
  static Future<Null> callWithParams(String id) async {
    try {
      await myMethodChannel.invokeMethod('methodOne', {'id': id});
    } on PlatformException catch (e) {
      print("${e.message}");
    }
  }

  static Future<Null> callWithoutParams() async{
    try {
      await myMethodChannel.invokeMethod('methodTwo');
    } on PlatformException catch (e) {
      print("${e.message}");
    }
  }

        MethodChannel(getFlutterView(), "methodChannelName")
                .setMethodCallHandler(object : MethodCallHandler {
            override fun onMethodCall(call: MethodCall, result: Result) {
                if (call.method.equals("methodOne")) {
                    val myId: String = call.argument("id")
                    // do something on Android side
                } else if (call.method.equals("methodTwo")) {
                    // do something on Android side
                } else {
                    // error: method is not defined
                }
            }
        })