当前位置:网站首页>Dart series: creating a library package

Dart series: creating a library package

2022-06-24 01:12:00 Procedural stuff

brief introduction

stay dart In the system , Yes pubspec.yaml The application of files can be called package. and Libray package It's a special kind of package, This package can be relied on by other projects . This is what is commonly called a library .

If you want what you wrote dart The program can be uploaded to pub.dev On , Or provide it to others , Let's take a look at this article .

Library package Structure

Let's take a look at library package Structure :

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

This is the simplest Library package Structure , stay root Below directory , We have a pubspce.yaml file . And then there's another one lib The contents are library Code for .

Generally speaking lib The following libraries can be referenced externally . If it is library Internal documents , You can put lib/src Below directory , The file here means private Of , It should not be introduced by other programs .

If you want to src The package in is exported for external use , Can be in lib Below dart Use... In the document export, What will be needed lib export . So other users only need import This is just a file .

export An example of this is :

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 code above is flutter Of animation library .

Import library

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

import 'package:flutter/animation.dart';

If it is the import of internal files , Relative paths can be used . Only when importing external package You only need to add package: Prefix .

Conditional import and export library

because dart It is designed to work on different platforms , So a library You may need to import or export different files on different platforms library file , This is called conditional import and export .

For example, you can judge dart Kuo is io Library or html Library to choose to export different files :

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

It means , If in app Can be used in dart:io, Then export src/hw_io.dart.

If it can be used dart:html, Then export src/hw_html.dart, Otherwise, export src/hw_none.dart.

If it is conditional import , take export Change to import that will do .

Add other valid files

Because it's different library It has different functions , So you usually need to add some additional files to ensure library Effectiveness and integrity of .

In order to ensure library The effectiveness of the , You need to add test code , Test code is usually placed in test Directory .

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

And then there is README.md and CHANGELOG.md Wait for the documents .

library Documents

dart Documents can be used dartdoc This tool is used to generate .dart The document format in is in /// At the beginning , as follows :

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

Publish to pub.dev

One of the best to share library The way is to send it to pub.dev On . The specific order is :pub publish.

summary

That's all dart Created in library The whole content of .

This article has been included in http://www.flydean.com/11-dart-create-package/ The most popular interpretation , The deepest dry goods , The most concise tutorial , There are so many tricks you don't know about waiting for you to discover !

原网站

版权声明
本文为[Procedural stuff]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/11/20211120085508838N.html