Check Package’s website pub.dev and its domestic mirror

The Dart ecosystem uses packages to manage shareware, such as libraries and tools. Use Pub (package management tool) to get packages.

Pub supports loading and version-dependent management of available packages from local file systems or other locations, such as Git repositories, to help us get version-compatible packages and SDKS.

The package directory contains at least one pubspec(pubspec.yaml) file. The PubSpec file records some metadata about the package. In addition, the package contains additional dependencies (listed in PubSpec), the Dart library, applications, resources, tests, images, and samples.

Use the Package

To use a reference package, do the following:

  1. To create apubspec(pubspec.yaml).
name: test

environment:
  sdk: "> = 2.10.0 < 3.0.0"

dependencies:
  english_words: ^ 4.0.0 # rely on english_words
Copy the code
  1. usePubGets the package currently dependent.
#Run the command line on the terminal
#After running, the pubspec.lock file is generated to lock the dependent version
dart pub get
Copy the code
  1. If the current code depends on a library in the package, import that library.
import 'package:english_words/english_words.dart';

main() {
  generateWordPairs().forEach(print);
}
Copy the code
  1. If you need to upgrade the package version, you need to upgrade the dependency.

The upgrade dependency command does not always update all packages to the latest version due to version-limiting conflicts between some packages in pubSpec files. To determine packages that are outdated and need to be edited, use the dart pub outdated command.

#Prepare to update dependencies to the latest version
$ dart pub upgrade

Copy the code

Create a Package

This article will show you how to create a Package using the most common Library Package.

# Library Package file structure
mylib
  - lib/
    - mylib.dart        # header file, which exports all public apis
    - mylib_mini.dart   # Mini Library header file, general small projects do not use
    - src/              SRC files should not be imported
      - mylib_imp1.dart # concrete implementation
      - mylib_imp2.dart
      - .
  - test/               # Test cases
  - pubspec.yaml        # declaration file
Copy the code

Using the part command at the head of the file, you can split a Library into multiple Dart files. Here, we recommend that you create the Mini Library instead of using the part command.

Logger

The demo library yxlogger

├ ─ ─ CHANGELOG.md                # CHANGELOG
├ ─ ─ LICENSE                     # LICENSE
├ ─ ─ README.md                   # README
├ ─ ─ lib
   ├ ─ ─ yxlogger.dart           # header file
   └ ─ ─ src
       ├ ─ ─ error_logger.dart   # ErrorLogger implementation
       ├ ─ ─ logger.dart         Logger abstract class/interface
       └ ─ ─ stdin_logger.dart   # StdinLogger implementation
├ ─ ─ pubspec.lock                # Pub Automatically generates files
├ ─ ─ pubspec.yaml                # Pub Describe the file
└ ─ ─ test
    ├ ─ ─ error_logger_test.dart  ErrorLogger unit test
    └ ─ ─ stdin_logger_test.dart  # StdinLogger unit test
Copy the code

Creating a YxLogger

  1. Create a new file according to the directory.
  2. The editorpubspec.yamlAnd then rundart pub getGet dependencies.
name: yxlogger # the name
description: yxloggers # description
version: 1.0. 0 # version number
homepage: https://github.com/tangbl93/yxlogger

environment: # Dart version dependency
  sdk: "> = 2.12.0 < 3.0.0"

dev_dependencies: # Test dependencies for units
  test: ^ 1.16.8
Copy the code
  1. writeLoggerAbstract classes/interfaces and their corresponding implementations.
// Logger
abstract class Logger {
  String log(String msg);
}

// StdinLogger / ErrorLogger
import 'package:yxlogger/src/logger.dart';

class StdinLogger implements Logger {
  @override
  String log(String msg) {
    return "StdinLogger: $msg"; // ErrorLogger: return "ErrorLogger: $msg";}}Copy the code
  1. Write the header file.
library yxlogger;

export 'src/stdin_logger.dart';
export 'src/error_logger.dart';
Copy the code
  1. Writing unit tests
// Rely on test for unit tests
import 'package:yxlogger/yxlogger.dart';
import 'package:test/test.dart';

void main() {
  var logger = StdinLogger();
  group('StdinLogger', () {
    test('hi', () {
      expect(logger.log("hi"), "StdinLogger: hi");
    });
    test('hey', () {
      expect(logger.log("hey"), "StdinLogger: hey");
    });
  });
}

Copy the code

Release the Package

Publish is a command in Pub. This command is used to publish the Package to pub.dev for others to download and rely on.

Before publishing, you can check the process to see what is not currently ready.

#This option allows you to run the entire process of uploading the Package without actually uploading any files to the pub.dev site.
#This allows you to check your upload configuration before you actually upload to pub.dev.
$ dart pub publish --dry-run  # --dry-run option or -n option
Copy the code

If the check process is ok, or if a warning appears, you can use this command to upload.

You may need to log into your Google account to upload. Notice the console output.

When uploading, you cannot upload normally and encounter an error, you can try to add sudo to run. SDK issues#16658

$ dart pub publish --verbose
Copy the code

If there is an error in the Package, Pub will exit and not proceed with the upload. If you want to continue uploading at this point, you can do so.

#This option lets Pub no longer confirm with you when uploading. Normally, it will show you the contents of the Package and give you confirmation when you upload.
$ dart pub publish --force # --force option or -f option
Copy the code

Refer to the link

  • Packages