In Harmony Flutter development, if it involves using native functions, plug-ins must be used. There are two ways to use plug-ins. One is to write native ArkTS code yourself and call it on the Dart side. The other is to use third-party code.
Method 1: Write native ArkTS code
This solution can be called using PlatformView or MethodChannel.
PlatformView is to create a View on the Flutter side and then render it on the Native side. PlatformView encapsulates the underlying View.
MethodChannel is to call the native Native method through MethodClannel.
For specific operations, please refer to the articles Integrating Webview in Harmony Flutter Development and Using ArkTs to Develop Flutter Harmony platform plug-in](https://gitee.com/zacks/awesome-harmonyos-flutter/blob/master/%E9%B8%BF%E8%92%99Flutter%E5%AE%9E%E6%88%98%EF%BC%9A06-%E4%BD%BF%E7%94%A8ArkTs%E5%BC%80%E5%8F%91Flutter%E9%B8%BF%E8%92%99%E6%8F%92%E4%BB%B6.md)
Method 2: Use third-party code
Find the plug-in to be used in pub.flutter.dev/github/gitee/ophm. If the plug-in has been adapted to Harmony, it can be used normally like other Flutter plug-ins.
If the plug-in has not been adapted to Harmony, you need to find an adapted plug-in library. The configuration method is as follows
If the third-party plug-in used is not compatible with Harmony, you need to configure its Harmony alternative plug-in through overrider, otherwise an error will be reported at runtime. As shown below:
dependencies:
path_provider: ^2.1.0
dependency_overrides:
# ohos
path_provider:
git:
url: "https://gitee.com/openharmony-sig/flutter_packages.git"
path: "packages/path_provider/path_provider"
It should be noted here that if there is no dependency conflict, dependency_overrides may not take effect. In other words, if you check the pubspec.lock file and find that the dependent plug-in library does not exist in the **_ohos library, it means that overrides are not effective. At this time, use the following method to modify the pubspec_overrides.yaml file and manually add the file.
If overrides do not work, open pubspec_overrides.yaml, add the following content, run pub get again, and you will find that pubspec.lock has successfully added the **_ohos library.
dependency_overrides:
# ohos
path_provider:
git:
url: "https://gitee.com/openharmony-sig/flutter_packages.git"
path: "packages/path_provider/path_provider"
In addition, if you do not find the Harmony plug-in you are using, you can consider writing your own cross-end call code, or writing a new plug-in library as a specific platform implementation of the original plug-in library.
Top comments (0)