Skip to content

Instantly share code, notes, and snippets.

@0phelia
Created June 18, 2018 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0phelia/7e96707c21c73eb3d6ff8753bef16544 to your computer and use it in GitHub Desktop.
Save 0phelia/7e96707c21c73eb3d6ff8753bef16544 to your computer and use it in GitHub Desktop.
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
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment