Dart series: create library package

Time:2022-8-2

brief introduction

In dart system, there is pubspec The application of yaml file can be called a package. And libray package is a special kind of package, which can be relied on by other projects That is commonly referred to as the library.

If you also want to upload your dart program to pub.dev or provide it to others, take a look at this article.

Structure of library package

First look at the structure of the library package:

app3
├── lib
│   └── main.dart
└── pubspce.yaml

This is the simplest structure of library package. Under the root directory, we have a pubspce.yaml file. Then there is a lib directory where the library code is stored.

Generally speaking, the Libraries under lib can be referenced externally. If it is an internal file of library, you can put it under the lib/src directory. The files in this directory indicate that they are private and should not be introduced by other programs.

If you want to export the package in SRC for external use, you can use export in the dart file under lib to export the needed lib. In this way, other users only need to import this file.

An example of export is as follows:

library animation;

export 'src/animation/animation.dart';
export 'src/animation/animation_controller.dart';
export 'src/animation/animations.dart';
export 'src/animation/curves.dart';
export 'src/animation/listener_helpers.dart';
export 'src/animation/tween.dart';
export 'src/animation/tween_sequence.dart';

The above code is the animation library of flutter.

Import library

How to use it? We can use the import statement to import the corresponding lib:

import 'package:flutter/animation.dart';

If you are importing an internal file, you can use a relative path. Package: prefix is only required when importing external packages.

Conditional import and export Library

Because dart is designed to work on different platforms, a library may need to import or export different library files on different platforms, which is called conditional import and export.

For example, you can choose to export different files by judging whether the dart library is an IO library or an HTML Library:

export 'src/hw_none.dart' // Stub implementation
    if (dart.library.io) 'src/hw_io.dart' // dart:io implementation
    if (dart.library.html) 'src/hw_html.dart'; // dart:html implementation

The above means that if dart:io can be used in the app, src/hw will be exported_ io.dart.

If you can use dart:html, export src/hw_ Html.dart, otherwise export src/hw_ none.dart。

If it is conditional import, change export to import.

Add other valid files

Because different libraries have different functions, it is usually necessary to add some additional files to ensure the effectiveness and integrity of the library.

To ensure the effectiveness of the library, you need to add test code, which is usually placed in the test directory.

If you are creating a command-line tool, you need to put the corresponding tool in the tools directory.

There is also readme MD and changelog.md.

Library documentation

Dart documents can be generated using dartdoc. The document format in dart starts with / /, as follows:

/// The event handler responsible for updating the badge in the UI.
void updateBadge() {
  ...
}

Publish to pub.dev

One of the best ways to share a library is to send it to pub Dev. The specific command is: pub publish.

summary

The above is all about creating a library in dart.

This article has been included inhttp://www.flydean.com/11-dart-create-package/

The most popular interpretation, the most profound dry goods, the most concise tutorials, and many tips you don’t know are waiting for you to find!

Welcome to my official account: “program things”, understand technology, and understand you better!