Master Bi-Directional Communication in Google Flutter
Table of Contents
- Introduction
- Setting up the Flutter Project
- Communicating with the Native Platform
- Calling Native Functions in Android
- Calling Native Functions in iOS
- Handling Exceptions from the Native Side
- Calling Flutter Functions from Native Side
- Conclusion
Introduction
In this tutorial, we will explore how to implement native communication in Flutter. We will learn how to make calls to native functions in Android and iOS platforms. We will begin by setting up a Flutter project and then proceed to establish communication with the native platform. Next, we will cover how to call native functions in both Android and iOS. We will also discuss exception handling from the native side and how to call Flutter functions from the native side. By the end of this tutorial, You will have a clear understanding of how to communicate between Flutter and native platforms.
Setting up the Flutter Project
To begin, we'll Create a new Flutter project and set up the necessary dependencies. We will start with an empty template and gradually build upon it. First, we'll create a simple UI for our demo, consisting of a home screen with a navigation bar and a body containing a button to get a message from the native platform. We'll write the necessary functions and widget declarations to handle communication with the native platform. Furthermore, we'll make sure to update the window title and run the app to see our initial UI.
Communicating with the Native Platform
To establish communication with the native platform, we will use a method Channel. The method channel allows us to communicate with the native platforms, such as Android and iOS. We will define a method name that will serve as a signature to be used on both the Flutter and native sides. We will implement a function, "GetMessageFromNative," which will be an asynchronous function that assigns the result from the native platform to a message variable using the "invokeMethod" function. We will also handle possible exceptions using a try-catch block. When the button in the UI is tapped, we will call this method and use "setState" to update the message in the UI.
Calling Native Functions in Android
In order to call native functions in Android, we need to make some changes to the Android project. We will add a FlutterEngine and FlutterMethodChannel to the App Delegate file, which is the entry point of an iOS application. We will set up a method called "HandleMethod" to handle the method called from the Flutter side, and Based on the method name, we can call any Flutter function or perform any processing. We will demonstrate how to receive the message from the native side by decoding the arguments as a map of STRING-dynamic and getting the message value from the JSON. We will initialize the Flutter channel and method handler in the init state of the Flutter application. Finally, we will display the message in the UI.
Calling Native Functions in iOS
Similar to Android, we will make changes to the iOS project to call native functions in iOS. We will create a method called "configurePltaeEngine" to initialize our Flutter channel and binary messenger for communication between Flutter and the native platform. We will also implement the method called handler, just as we did on the native side. This method will handle the method name and arguments passed from the Flutter side. Within the switch statement, we can match the method name and call any Flutter function or perform any processing. We will get the message from the native side and decode the arguments as a map of string-dynamic. We will call this "initNativeMethodHandler" function in the init state to initialize the Flutter channel for receiving callbacks from the native side.
Handling Exceptions from the Native Side
To handle exceptions from the native side, we will wrap the "invokeMethod" function with a try-catch block. If an exception occurs, we will assign the message to "failedToCallGetMessage" with the exception object. This will allow us to catch exceptions from the native side and handle them accordingly. We will demonstrate how platform exceptions can be triggered by sending an error from the native side and how to handle them in the Flutter application. We will also provide a detailed description and error code in the Flutter error object.
Calling Flutter Functions from Native Side
To call Flutter functions from the native side, we will utilize the "invokeMethod" function. We need to specify the method name and any arguments that need to be passed. We will set up methods on the Flutter side to receive the callback from the native side. We will initialize the Flutter channel to get the callback from the native side. Within the method handler, we will match the method name and call the corresponding Flutter function. We will demonstrate how to call Flutter functions from both Android and iOS platforms. Additionally, we will highlight the importance of running the code inside the main thread to ensure proper communication between the native and Flutter platforms.
Conclusion
In this tutorial, we have explored how to implement native communication in Flutter. We have learned how to make calls to native functions in both Android and iOS platforms. We have discussed the necessary setup and code changes required to establish communication with the native platform. Additionally, we have covered exception handling from the native side and how to call Flutter functions from the native side. By following this tutorial, you now have the knowledge and understanding to effectively communicate between Flutter and native platforms, opening up a wide range of possibilities for your Flutter applications.