MethodChannel enables sending messages that correspond to method calls. On the platform side, MethodChannel on Android (MethodChannelAndroid) enable receiving method calls and sending back a result. These classes allow you to develop a platform plugin with very little ‘boilerplate’ code.
You can check the Flutter official documentation for platform channels.\
You can check our Portal Flutter example.
Add Portal implementation for Android:
- Open the Android host portion of your Flutter app in Xcode.
- Add
Portal Android SDKto the Android project, you can follow this Android setup guide. - Open
MainActivity.ktlocated under android > app > src > main > kotlin in the project navigation.
your.bundle.identifier/portal. Below the channel name and some method names are defined.
MainActivity.kt, configure channel and methods handling like this.
FlutterResult which is expected to be passed from Flutter side as a completion handler.
You will also need to define this implementation in your native iOS code as well for the code to work on both iOS and Android. Refer to this guide for iOS specific instructions.
Calling platform-specific code using platform channels
Now Portal Android implementation is ready for Flutter, Let’s create the Flutter platform client:We will use the same channel name we defined above to initialize
MethodChannel
String identifier initializePortal. The call might fail, so wrap the invokeMethod call in a try-catch statement.
initializePortal to initialize portal is ready to be called. You can call it from Flutter side passing the apiKey to initialize Portal.