Skip to main content

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. 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:

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.

Additionally enable the usage of Swift Package Manager, as the native iOS native library is using it for library integration:

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 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.
    • An example of the native build.gradle.kts file, and how the library is integrated, can be found here.
    • An example of the Capture Flutter library build.gradle.kts file can be found here.

iOS

  1. Go to the microblink_platform/ios/microblink_platform/Package.swift file

  2. Add the following content to the file:

    // 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.

  1. In the microblink_platform.podspec file, add the following:

    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:

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.
    • The iOS SDK instructions can be found here, and the sample application here.
  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 and Android.
  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, capture_flutter_platform_interface, and lastly capture_flutter.dart.

Using the Capture SDK as an example, let's say we want wrap the AnalyzerSettings object.

  1. Create the class in Dart, as seen here.

  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 in the CaptureSerializationUtils.kt file.
    • This method is later called in the CaptureFlutterPlugin.kt file here with the CaptureSettings object.
  3. For iOS, you will need to create a method that returns a MBCCAnalyzerSettings object. It takes a Dictionary<String, Any> in its arguments.

    • An example of this can be seen here in the CaptureSerializationUtils.swift file.
    • This method is later called in the CaptureFlutterPlugin.swift file, here.
  4. The usage in Dart can be found here in the sample application.

Using the Capture SDK as an example, let's say we want to get the AnalyzerResult object.

  1. Create the class in Dart, as seen here.

  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 in the CaptureSerializationUtils.kt file.
    • This method is later called in the CaptureFlutterPlugin.kt file here 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 in the CaptureSerializationUtils.swift file.
    • This method is later called in the CaptureFlutterPlugin.swift file, here 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.
  • 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.