During the reconstruction of mobile terminal, our team chose Flutter to carry out the reconstruction of mobile terminal in terms of technology selection, because Flutter can fully save human efficiency in UI development efficiency and more time can be spent on other aspects of research. However, in actual development, some functions of Flutter are not supported, and the Native terminal is also required to support them. How to make the functions supported by the Native terminal convenient and efficient to serve ourselves while providing convenience to the outside world? There is no need to build wheels repeatedly when other projects or teams need to use the same functionality, such as Java JAR, Android AAR, Web NPM, iOS POD, etc., but Flutter, Flutter also supports the use of shared software packages contributed by other developers to the Flutter and Dart ecosystems, allowing us to quickly build applications without having to develop all applications from scratch, which is the Package we will introduce today.

Therefore, the understanding of package should not only stop at the level of being able to use it, but also need to know how to develop a package of your own and how to successfully publish your package to pub warehouse or private warehouse for others to use.

01 package introduced

What is a Package

Using packages you can create modular code that can be easily shared, and a minimal Package includes:

  • A pubspec.yaml file: a metadata file that declares the name, version, author, and so on of the package.
  • A lib folder: include the public code in the package, and at least one.dart file.

Package type:

Flutter Package: The Flutter pure DART plugin project contains only the implementation of the DART layer, often defining some common widgets

The Flutter Plugin is a dedicated Dart package that contains apis written in Dart code and specific implementations for Android (using Java or Kotlin) and iOS (using OC or Swift) platforms

A Plugin is simply a special Package. The Flutter Plugin provides the underlying encapsulation of Android or iOS and provides component functions on the Flutter layer, enabling Flutter to easily access Native modules. Many platform-dependent or complex aspects of Flutter implementation can be encapsulated as plugins. The communication principle between Flutter and Native will not be discussed in depth in this article. The communication principle can be referred to the following figure:

02 package development

Now that we know what Package is, how do we develop our own Package?

There are generally two ways to create a package or plug-in:

  • Visual mode
  • Command-line mode

Visual mode

  • Open Android Studio -> Select Create New Flutter Project;

  • To create Plugin packages, select the Flutter Plugin;
  • Select Flutter Package if you want to create Dart Package.

  • Modify the content as needed, then click Next;

  • Modify the package name as required and select the language required in package development (this step will only be performed if you select pulgin package)

  • Creating a successful

Command-line mode

  • Create Dart package

flutter create –template=package dartPackageNam 

  • Create the plugin package

Flutter create –org com.example –template=plugin –platforms=android,ios -a kotlin -i swift pluginPackageName

–org: Specify your organization, using reverse domain name notation

–template: Specifies whether the Dart package is pure or Plugin

Platforms are available: Android, ios, Web, Linux, MacOS, and Windows

-a Specifies the language Java and Kotlin used by the Android platform

-i Specifies objc and Swift languages supported by the iOS platform

After the Package is successfully created, look at the files under the project:

The pure Dart package contains files:

  • LICENSE file # LICENSE file.
  • Test /flutter_package_first_test.dart file ## Unit test file for Package.
  • Gitignore file ## A hidden file that tells your Git system which files or folders to hide.
  • Pubspec. yaml file the file containing the YAML format that package depends on that the ## pub tool needs to use.
  • The readme.md file ## starter document is used to describe the package.
  • Dart ## dart implementation code for package lib/flutter_package_first.dart
  • The changelog. md file ## is used to record the version changes of the package

Plugin package contains the following files:

  • Dart file ## dart plugin API implementation.
  • Android/SRC/main/Java/com/example/flutter_plugin/FlutterPlugin file # # android native plug-in API implementation (using the Kotlin programming language).
  • Ios/Classes/FlutterPlugin. # # m file ios native plug-in API implementation (using the Objective – the C programming language).
  • Example/file ## a Flutter application that relies on this plugin and explains how to use it.

If you want to edit the relevant files in the iOS directory or the Android directory, you need to open the files at each end in a specific editor for editing

  • The iOS side:

  • The Android side:

03 package release

Now that we know what package packages are and how to make different packages, how do we distribute package packages? (Group PUB warehouse release documents)

The release location of package can be selected:

  • pub.dev 
  • Private PUB Warehouse (Group PUB Warehouse)
  • Git repository

Before publishing the package, check these files: pubspec.yaml, readme. md, changelog.md to make sure they are complete.

  • Configuration of pubspec.yaml configuration file:

1. Name: XXXX ## pub Library name

2. Version: 0.0.1 ## The current version of the plug-in ensures that each release is inconsistent with the previous version

3. Description: XXXXXXXXX ## pub

4. Homepage :github.com/xxxx ## pub git address

5. Publish_to:pub.100tal.com/ ## private pub library upload address if not private pub warehouse need not configure

6. Author :[email protected] ## pub

  • Detect configuration file:

flutter packages pub publish –dry-run 

  • release

flutter packages pub publish 

04 package 

Once the package has been successfully published, how do we use it in our projects?

The first step is to add dependencies to pubspec.yaml

  • Depends on the package package of the pub.dev repository

In this project, the plug-in specifies version 1.4.0

Flutter_plugin: ^ 1.4.0

In this project, the lowest version of this plug-in is 1.2.3

Flutter_plugin: “> = 1.2.3”

In this project, the plug-in is supported at a minimum of 2.0.0, but not above 3.0.0

Flutter_plugin: “> = 2.0.0 < 3.0.0”

In this project, the plug-in is supported up to version 1.2.3

Flutter_plugin: “” 1.2.3″

  • Depend on git repository’s package

flutter_plugin: 

Git: url: github.com/xxxxxx/xxxx… Git repository address

If the project is not in the root directory of the git address, you need to specify path

Ref: ‘1.0.0’ # specifies a git repository tag

  • Depend on the package package of the private PUB repository

xes_recorder: 

hosted: 

name: xes_recorder  

url: pub.100tal.com 

Version: 0.0.1

  • Relying on local packages

flutter_pub: 

path: .. / # can be a relative path or an absolute path

05 Records of problems that may occur during release

1) The author will warn that the flutter packages pub publish –dry-run detection should be removed to pass the detection in the latest version.

2) When executing the issue command, you will need to log in to the following address for authorization. If you need to log in to Google account, please use Chrome to open it for verification.

  • Go to pub.dev or your own private pub repository to check whether the publication is successful.

  • Verify successful – upload starts – if the timeout fails, re-upload will need to bypass Google authorization.

3) Skip Google validation;

  • Download pub project [download address: github.com/ameryzhu/pu…
  • Open the downloaded project with Android Studio and execute it in Terminal order:

1. The pub get; dart–snapshot=mypub.

Dart. Snapshot bin/pu b.dart # mypub.dart. Snapshot file is generated in the pub root directory.

${flutterSDK Path}/bin/cache /dart-sdk/bin/snapshots/

${flutterSDKPath}/bin/cache /da rt-sdk/bin/pub;

6) Replace the third from last line of the file with mypub.dart.snapshot;

7) Save-exit

8) Run the issue command again

If you do this, there will be a version-conflicting error when executing pub get in your project. Revert mypub.dart.snapshot to pub.dart.snapshot in the pub file modified by the above method.

On April 23, the world Book Day, the wechat official account will send books for free

Scan the wechat official account of “Good Future Technology” and reply “Send books” to participate in this activity

Exclusive pet fan welfare, still waiting for what to pay attention to quickly