CodeNewbie Community 🌱

Cover image for HarmonyOS Flutter in action: 05 - Use third-party plugins
shaohushuo
shaohushuo

Posted on

HarmonyOS Flutter in action: 05 - Use third-party plugins

In HarmonyOS Flutter development, if it comes to using native features, you need to use plugins. There are two ways to use plug-ins, one is to write the 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 scheme can be called using PlatformView or MethodChannel.

  1. PlatformView is to create a View on the Flutter side and then render it on the Native side. PlatformView encapsulates the underlying View.

  2. MethodChannel calls the native method through MethodCannel.

For specific operations, please refer to the articles Integrating Webview in Harmony Flutter Development and [Developing Flutter with ArkTs] respectively Harmony Platform Plugin] (https://gitee.com/zacks/awesome-harmonyos-flutter/blob/master/ Harmony Flutter Practice: 06 - Use ArkTs to Develop Flutter Harmony Plugin.md)

Method 2: Use third-party code

  1. Find the plugin you use in the pub.flutter.dev/github/gitee/ophm, if the plugin has been adapted to HarmonyOS, it can be used normally like other Flutter plugins.

  2. If the plug-in has not yet adapted to HarmonyOS, you need to find an adapted plug-in library. The following describes how to configure the configuration

  3. If you use a third-party plug-in that does not adapt to HarmonyOS in the underlying library, you need to configure its HarmonyOS replacement plug-in through Overrider, otherwise it will be incorrect at runtime. As shown below:

dependencies:
  path_provider: ^2.1.0

dependency_overrides:
  # ohos
  path_provider:
    git:
      url: "https://gitcode.com/openharmony-sig/flutter_packages.git"
      path: "packages/path_provider/path_provider"
Enter fullscreen mode Exit fullscreen mode

It is important to note here that if there is no dependency conflict, dependency_overrides may not take effect. That is to say, if you check the pubspec.lock file and find that the dependent plug-in library does not exist, and the **_ohos library does not exist, it means that overrides does not take effect pubspec_overrides.

If overrides doesn't take effect, open pubspec_overrides.yaml, add the following, run pub get again, and find that pubspec.lock has successfully added the **_ohos library.

dependency_overrides:
  # ohos
  path_provider:
    git:
      url: "https://gitcode.com/openharmony-sig/flutter_packages.git"
      path: "packages/path_provider/path_provider"
Enter fullscreen mode Exit fullscreen mode

In addition, if you don't find a HarmonyOS plugin to use, you can consider writing your own code for the crash call, or writing a new plugin library as a platform-specific implementation of the original plugin library.

References

Top comments (0)