Skip to content

Instantly share code, notes, and snippets.

@romaomb
Last active May 21, 2020 13:03
Show Gist options
  • Save romaomb/38eccbdc21690a047c846bc18a7aea75 to your computer and use it in GitHub Desktop.
Save romaomb/38eccbdc21690a047c846bc18a7aea75 to your computer and use it in GitHub Desktop.
MethodChannel's sample implementation
import 'package:flutter/services.dart';
class MethodChannelSample {
final _channel = MethodChannel('codes.romao.method');
MethodChannelSample() {
_channel.setMethodCallHandler(_methodHandler);
}
Future<dynamic> _methodHandler(MethodCall call) async {
switch (call.method) {
case 'dartMethod':
return await _invokeDartMethod(call.arguments);
default:
_handleUnknownMethod(call.method);
break;
}
}
Future<bool> invokeNativeMethod() async {
try {
return await _channel.invokeMethod('nativeMethod', 'argument');
} catch (error) {
_handleNativeError(error.message);
}
return null;
}
Future<dynamic> _invokeDartMethod(dynamic arguments) async {}
void _handleUnknownMethod(String methodName) {}
void _handleNativeError(String errorMessage) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment