--- Source: https://docs.microblink.com/platform/sdk/flutter-wrapper Title: Flutter wrapper Description: Wrap Microblink Platform native SDKs for Flutter cross-platform development --- # Flutter wrapper The purpose of this document is to give a detailed overview on how to wrap native iOS and Android Microblink Platform SDKs so that they can be implemented in the Flutter environment. To see the best example of how a Microblink's native mobile SDKs are used for Flutter, please see the Capture Flutter SDK [here](https://github.com/BlinkID/capture-flutter). All of the examples in this document will be compared to this SDK. ## Creating the library To create a new Flutter library, run the following command: ```bash flutter create --org com.microblink --template=plugin --platforms=android,ios -a kotlin -i swift microblink_platform ``` This will create the `microblink_platform` folder, which consist of: 1. The `android` folder, where all of the native Android (Kotlin) code is stored. 2. The `ios` folder, where all of the native iOS (Swift) code is stored. 3. The `lib` folder, where all of the Dart code is stored. As an example, see the Capture Flutter SDK structure [here](https://github.com/BlinkID/capture-flutter/tree/main/Capture). Additionally enable the usage of Swift Package Manager, as the native iOS native library is using it for library integration: ```bash cd microblink_platform flutter config --enable-swift-package-manager ``` ## Adding native SDKs to the project ### Android 1. Firstly, clone the native Android SDK: ``` git clone https://github.com/MicroblinkPlatform/microblink-platform-android.git ``` :::note Make sure you have [Git LFS](https://docs.github.com/en/repositories/working-with-files/managing-large-files/installing-git-large-file-storage) installed, as the libraries are on Git Large File Storage. ::: 2. Add the libraries from the GitHub repository in the created Flutter library. - The instructions on how to add the library manually in the `build.gradle.kts` file can be found [here](https://github.com/MicroblinkPlatform/microblink-platform-android?tab=readme-ov-file#-sdk-integration). - An example of the native `build.gradle.kts` file, and how the library is integrated, can be found [here](https://github.com/MicroblinkPlatform/microblink-platform-android/blob/main/MicroblinkPlatformSample/app/build.gradle.kts). - An example of the Capture Flutter library `build.gradle.kts` file can be found [here](https://github.com/BlinkID/capture-flutter/blob/main/Capture/android/build.gradle). ### iOS 1. Go to the `microblink_platform/ios/microblink_platform/Package.swift` file 2. Add the following content to the file: ```swift // swift-tools-version: 5.9 import PackageDescription let package = Package( name: "microblink_platform", platforms: [ .iOS(.v16), ], products: [ .library(name: "microblink-platform", targets: ["microblink_platform"]) ], dependencies: [ .package(url: "https://github.com/MicroblinkPlatform/microblink-platform-ios.git", branch: "main") ], targets: [ .target( name: "microblink_platform", dependencies: [ .product(name: "MicroblinkPlatform", package: "microblink-platform-ios") ], resources: [] ) ] ) ``` In case there are any issues with the Swift package setup, run the `swift package resolve` command where the `Package.swift` file is located. 3. In the `microblink_platform.podspec` file, add the following: ```ruby Pod::Spec.new do |s| s.name = 'microblink_platform' s.version = '0.0.1' s.summary = 'A new Flutter plugin project.' s.description = <<-DESC A new Flutter plugin project. DESC s.homepage = 'http://example.com' s.license = { :file => '../LICENSE' } s.author = { 'Your Company' => 'email@example.com' } s.source = { :path => '.' } s.source_files = 'microblink_platform/Sources/plugin_name/**/*.swift' s.dependency 'Flutter' s.platform = :ios, '16.0' # Flutter.framework does not contain a i386 slice. s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'i386' } s.swift_version = '5.0' # If your plugin requires a privacy manifest, for example if it uses any # required reason APIs, update the PrivacyInfo.xcprivacy file to describe your # plugin's privacy impact, and then uncomment this line. For more information, # see https://developer.apple.com/documentation/bundleresources/privacy_manifest_files # s.resource_bundles = {'microblink_platform_privacy' => ['microblink_platform/Sources/microblink_platform/PrivacyInfo.xcprivacy']} end ``` ## Verifying the integration of native SDKs You can check if the native SDKs were properly added by going to your `pubsec.yaml` file and adding the dependency: ```yaml dependencies: flutter: sdk: flutter microblink_platform: path: ../path-to-the-microblink_platform-folder ``` And then run the `flutter pub get` command. ## Creating method channels for Flutter Platform channels are the "connecting point" of the communication between Flutter and the native SDKs. 1. Take a look at how the native SDKs are integrated and used: - The Android SDK instructions can be found [here](https://github.com/MicroblinkPlatform/microblink-platform-android?tab=readme-ov-file#sdk-integration). - The iOS SDK instructions can be found here, and the sample application [here](https://github.com/MicroblinkPlatform/microblink-platform-ios/tree/develop?tab=readme-ov-file#uikit). 2. This native code will need to be implemented in the `MicroblinkPlatformPlugin.kt` file for Android, and `MicroblinkPlatformPlugin.swift` for iOS. - As an implementation example, see how the native code was implemented here for the Capture SDK, both for [iOS](https://github.com/BlinkID/capture-flutter/blob/main/Capture/ios/Classes/CaptureFlutterPlugin.swift) and [Android](https://github.com/BlinkID/capture-flutter/blob/main/Capture/android/src/main/kotlin/com/microblink/capture/flutter/CaptureFlutterPlugin.kt). 3. The code that connects the native code with Flutter & Dart, can be implemented in the following Dart files: - `microblink_platform_method_channel.dart` - `microblink_platform_platform_interface.dart` - `microblink_platform.dart` As an implementation example, please see the Dart implementation for the Capture SDK implementaion in [capture_flutter_method_channel.dart](https://github.com/BlinkID/capture-flutter/blob/main/Capture/lib/capture_flutter_method_channel.dart), [capture_flutter_platform_interface](https://github.com/BlinkID/capture-flutter/blob/main/Capture/lib/capture_flutter_platform_interface.dart), and lastly [capture_flutter.dart](https://github.com/BlinkID/capture-flutter/blob/main/Capture/lib/capture_flutter.dart). ## Example of wrapping a Microblink Platform setting for Dart Using the Capture SDK as an example, let's say we want to wrap the [AnalyzerSettings](https://github.com/BlinkID/capture-flutter/blob/main/Capture/lib/capture_settings.dart#L33) object. 1. Create the class in Dart, as seen [here](https://github.com/BlinkID/capture-flutter/blob/main/Capture/lib/capture_settings.dart#L33-L128). 2. For Android, you will need to create, for example, a method that returns the `AnalyzerSettings` object, which is used by the native SDK. It takes a `JSONObject` object in its arguments. - An example of this can be seen [here](https://github.com/BlinkID/capture-flutter/blob/main/Capture/android/src/main/kotlin/com/microblink/capture/flutter/CaptureSerializationUtils.kt#L57-L128) in the `CaptureSerializationUtils.kt` file. - This method is later called in the `CaptureFlutterPlugin.kt` file [here](https://github.com/BlinkID/capture-flutter/blob/main/Capture/android/src/main/kotlin/com/microblink/capture/flutter/CaptureFlutterPlugin.kt#L53) with the `CaptureSettings` object. 3. For iOS, you will need to create a method that returns a `MBCCAnalyzerSettings` object. It takes a `Dictionary` in its arguments. - An example of this can be seen [here](https://github.com/BlinkID/capture-flutter/blob/main/Capture/ios/Classes/CaptureSerializationUtils.swift#L88-L141) in the `CaptureSerializationUtils.swift` file. - This method is later called in the `CaptureFlutterPlugin.swift` file, [here](https://github.com/BlinkID/capture-flutter/blob/main/Capture/ios/Classes/CaptureFlutterPlugin.swift#L32). 4. The usage in Dart can be found here in the [sample application](https://github.com/BlinkID/capture-flutter/blob/main/sample_files/main.dart#L40). ## Example of wrapping a Microblink Platform result for Dart Using the Capture SDK as an example, let's say we want to get the [AnalyzerResult](https://github.com/BlinkID/capture-flutter/blob/main/Capture/lib/capture_analyzer_result.dart#L7C7-L7C21) object. 1. Create the class in Dart, as seen [here](https://github.com/BlinkID/capture-flutter/blob/main/Capture/lib/capture_analyzer_result.dart#L7-L42). 2. For Android, you will need to create a method that returns a `JSONObject` object, which is used by Flutter. The method has the `AnalyzerResult` in its arguments. - An example of this can be seen [here](https://github.com/BlinkID/capture-flutter/blob/main/Capture/android/src/main/kotlin/com/microblink/capture/flutter/CaptureSerializationUtils.kt#L23-L55) in the `CaptureSerializationUtils.kt` file. - This method is later called in the `CaptureFlutterPlugin.kt` file [here](https://github.com/BlinkID/capture-flutter/blob/main/Capture/android/src/main/kotlin/com/microblink/capture/flutter/CaptureFlutterPlugin.kt#L77-L81) by sending the results via the `Result` object to Flutter. 3. For iOS, you will need to create a method that returns a `String` object, and has the `MBCCAnalyzerResult` it its arguments. - An example of this can be seen [here](https://github.com/BlinkID/capture-flutter/blob/main/Capture/ios/Classes/CaptureSerializationUtils.swift#L11-L32) in the `CaptureSerializationUtils.swift` file. - This method is later called in the `CaptureFlutterPlugin.swift` file, [here](https://github.com/BlinkID/capture-flutter/blob/main/Capture/ios/Classes/CaptureFlutterPlugin.swift#L61) with the `FlutterResult` object that sends the information back to Flutter. ## Additional notes - You will also need to use the `build_runner` and `json_serializable` dependencies since they are needed for JSON and communication between the native platforms and Flutter. See the Capture SDK dependencies, as an [example](https://github.com/BlinkID/capture-flutter/blob/main/Capture/pubspec.yaml). - Once you create the necessary methods, you will need to go to the `microblink_platform` folder again and run the `flutter pub run build_runner build` command to generate the `.g.dart` files.