“This is the 16th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Introduction to the

In the DART system, applications that have pubspec.yaml files can be called a package. Libray Package is a special kind of package that can be relied on by other projects. This is commonly known as a library.

If you also want to upload your Dart application to pub.dev or make it available to others, check out this article.

Structure of the Library Package

The structure of the Library Package is as follows:

├── ├─ ├─ ├─ ├─Copy the code

This is the simplest Library package structure. In the root directory, we have a pubspce. Yaml file. There is also a lib directory that holds library code.

Libraries under Lib are generally available for external reference. If it is an internal library file, it can be placed in the lib/ SRC directory, where the file representation is private and should not be imported by other programs.

If you want to export packages from SRC for external use, you can use export in the dart file below lib to export the required 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';
Copy the code

The above code is the animation library for Flutter.

The import library

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

import 'package:flutter/animation.dart';
Copy the code

If you are importing internal files, you can use relative paths. The package: prefix is needed only when importing an external package.

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 deciding 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
Copy the code

If dart: IO is available in your app, export SRC /hw_io.dart.

Dart: HTML export SRC /hw_html.dart if you can use dart: HTML, SRC /hw_none.dart otherwise.

For conditional import, change export to import.

Add other valid files

Because different libraries serve different purposes, it is often necessary to add additional files to keep the library valid and complete.

To keep the Library valid, you need to add test code, usually in the Test directory.

If you want to create a command line tool, you need to place the tool in the Tools directory.

There are also readme. md and Changelog.md files.

The library documentation

Dart documents can be generated using the dart Doc tool. The document format in DART starts with /// as follows:

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

Published to the pub. Dev

One of the best ways to share your library is to send it to pub.dev. The command is pub publish.


That’s all about creating a Library in DART.

This article is available at www.flydean.com/11-dart-cre…

The most popular interpretation, the most profound dry goods, the most concise tutorial, many tips you didn’t know waiting for you to discover!

Welcome to pay attention to my public number: “procedures those things”, understand technology, more understand you!