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:
- The
androidfolder, where all of the native Android (Kotlin) code is stored. - The
iosfolder, where all of the native iOS (Swift) code is stored. - The
libfolder, 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
-
Firstly, clone the native Android SDK:
git clone https://github.com/MicroblinkPlatform/microblink-platform-android.gitnoteMake sure you have Git LFS installed, as the libraries are on Git Large File Storage.
-
Add the libraries from the GitHub repository in the created Flutter library.
iOS
-
Go to the
microblink_platform/ios/microblink_platform/Package.swiftfile -
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.
-
In the
microblink_platform.podspecfile, 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.
-
Take a look at how the native SDKs are integrated and used:
-
This native code will need to be implemented in the
MicroblinkPlatformPlugin.ktfile for Android, andMicroblinkPlatformPlugin.swiftfor iOS. -
The code that connects the native code with Flutter & Dart, can be implemented in the following Dart files:
microblink_platform_method_channel.dartmicroblink_platform_platform_interface.dartmicroblink_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.
Example of wrapping a Microblink Platform setting for Dart
Using the Capture SDK as an example, let's say we want wrap the AnalyzerSettings object.
-
Create the class in Dart, as seen here.
-
For Android, you will need to create, for example, a method that returns the
AnalyzerSettingsobject, which is used by the native SDK. It takes aJSONObjectobject in its arguments. -
For iOS, you will need to create a method that returns a
MBCCAnalyzerSettingsobject. It takes aDictionary<String, Any>in its arguments. -
The usage in Dart can be found here in the sample application.
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 object.
-
Create the class in Dart, as seen here.
-
For Android, you will need to create a method that returns a
JSONObjectobject, which is used by Flutter. The method has theAnalyzerResultin its arguments. -
For iOS, you will need to create, a method that returns a
Stringobject, and has theMBCCAnalyzerResultit its arguments.
Additional notes
- You will also need to use the
build_runnerandjson_serializabledependencies 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_platformfolder again and run theflutter pub run build_runner buildcommand to generate the.g.dartfiles.