Last active
May 21, 2020 13:03
-
-
Save romaomb/38eccbdc21690a047c846bc18a7aea75 to your computer and use it in GitHub Desktop.
MethodChannel's sample implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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