Introduction

In the dart system, an application with a pubspec.yaml file can be called a package. The Libray package is a special type of package that can be dependent on other projects. It is also commonly referred to as a library.

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

Library package structure

First look at the structure of the library package:

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

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

Generally speaking, the libraries under lib are available for external reference. If it is a file inside the library, it can be placed under the lib/src directory. The files inside 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 required lib. In this way, other users only need to import this one file.

Examples of export are 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 it is an internal file import, you can use a relative path. The 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. This 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, then export src/hw_io.dart.

If you can use dart:html, then export src/hw_html.dart, otherwise export src/hw_none.dart.

If it is a conditional import, just 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 validity and integrity of the library.

In order to ensure the validity 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.

In addition, there are files such as README.md and CHANGELOG.md.

library's documentation

Dart documents can be generated using the tool 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 the library is to send it to pub.dev. The specific command is: pub publish.

Summarize

The above is all the contents of the library created in dart.

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

The most popular interpretation, the most profound dry goods, the most concise tutorial, and many tips you don't know are waiting for you to discover!

Welcome to pay attention to my official account: "Program those things", know technology, know you better!


flydean
890 声望433 粉丝

欢迎访问我的个人网站:www.flydean.com