Ensuring Secure Invocation of Native Code in Flutter Using Pigeon
Credit to https://unsplash.com/photos/a-close-up-of-a-pigeon-on-a-ledge-b279NWxC_5k

Ensuring Secure Invocation of Native Code in Flutter Using Pigeon

Why to use Pigeon? :

  1. Automatic Code Generation: Pigeon automatically generates platform-specific code, reducing manual coding efforts and ensuring consistency across platforms.
  2. Type Safety: Pigeon provides strong typing support, which helps prevent runtime errors by catching type mismatches during compile time.
  3. Asynchronous Communication: With Pigeon, asynchronous communication between Flutter and native code is seamless, simplifying the handling of asynchronous operations.
  4. Reduced Boilerplate: Pigeon eliminates much of the boilerplate code required for method invocation and parameter passing, resulting in cleaner and more concise code.
  5. Enhanced Readability: By abstracting away platform-specific details, Pigeon promotes cleaner and more readable code, making it easier for developers to understand and maintain.
  6. Compatibility with IDEs: Pigeon integrates well with popular IDEs like Visual Studio Code and Android Studio, providing features such as code completion and syntax highlighting for improved developer productivity.
  7. Community Support: Pigeon is actively maintained and supported by the Flutter community, ensuring ongoing updates and improvements.
  8. Scalability: Pigeon facilitates scalable app development by offering a structured approach to communication between Flutter and native code, making it easier to manage as the project grows.
  9. Reduced Error Prone: By automating code generation and providing type safety, Pigeon helps reduce the likelihood of errors and bugs, resulting in more reliable applications.
  10. Future Compatibility: Pigeon is designed to adapt to future changes and updates in Flutter, providing a robust foundation for long-term app development projects.


How to use Pigeon? :

  1. Add the Pigeon dependency to your pubspec.yaml file.

dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.2
pigeon:        

  1. Create a new file named pigeon.dart and define model classes to pass data to the native app and receive responses.

import 'package:pigeon/pigeon.dart';

@HostApi()
abstract class MyDataApi {
  @async
  ResponseData sendData(MyData data);
}

class MyData {
  String? name;
  int? age;
}

class ResponseData {
  String? name;
}

        

3. Run the following command to generate necessary files (pigeon.m, pigeon.h, and pigeon.g.dart):

flutter pub run pigeon \
--input lib/pigeon.dart \
--dart_out lib/pigeon.g.dart \
--objc_header_out ios/Runner/pigeon.h \
--objc_source_out ios/Runner/pigeon.m        

4. Import pigeon.h and pigeon.m into your Xcode project and add pigeon.h to the Runner-Bridging-Header.

#import "pigeon.h"        

5. Create a new class in Xcode, CustomMyDataAPI, to handle the native API calls.

public class CustomMyDataAPI: NSObject, MyDataApi {
    public func sendDataData(_ data: MyData, completion: @escaping (ResponseData?, FlutterError?) -> Void) {
        let responseData = ResponseData.init()
        responseData.name = data.name! + "from native"
        completion(responseData,nil)
    }
}        

6. Import pigeon.g.dart into the Dart file where you want to invoke the native method.

7. Implement a function to invoke the native method.

Future<void> onClick() async {
    print("on click called");
    MyDataApi v = MyDataApi();
    MyData data = MyData();
    data.name = "john academy";
    data.age = 20;

    ResponseData response = await v.sendData(data);
    setState(() {
      val = response.name ?? "No value received";
    });
}        

Link to repository :

https://github.com/HimanDhawan/pigeon_flutter

To view or add a comment, sign in

Others also viewed

Explore content categories