Introduction

Library is a very useful way for dart to organize code. By defining different libraries, very useful dart code can be encapsulated and provided to other projects. Although we are free to use import or export to import and import library. But what is the most appropriate usage? Let's take a look together.

Use part and part of

Although many programmers hate to use part, dart does provide the function of part to split a large lib into multiple smaller files.

That's right, just like the Chinese meaning of part, part is used to split lib files.

part of indicates that the current file is part of another main file. part indicates that the main file is composed of referenced files.

Let's take an example, if there are three files student_age.dart, student_name.dart and student.dart.

The first two files are part of the latter one.

student_age.dart:

part of student;

int getAge(){
    return 18;
}

student_name.dart:

part of student;

String getName(){
    return "jack";
}

student.dart:

library student;

part 'some/other/student_age.dart';
part 'some/other/student_name.dart';

What's wrong with the code above?

The problem with the above code is that for student_age.dart, the part of only specifies the library it belongs to, but we will be confused when reading it, because we don't know where the specific library is.

So it should be written like this:

part of '../../student.dart';

files in src

By default, the src file in the lib directory is only used internally by the package and is not allowed to be called by external projects.

So we must not directly import the src file in the lib package.

lib file in package

For a package, the files in lib are files that can be exported, but it is best not to use absolute paths or relative paths to directly import files in lib when introducing packages.

Instead, use import 'package:'.

For example, suppose we have a library file with the following structure:

my_package
└─ lib
   └─ api.dart
   test
   └─ api_test.dart

api.dart is the file we want to export. If we need to reference api.dart in api_test.dart, there are two ways:

import 'package:my_package/api.dart';

and:

import '../lib/api.dart';

One of the above methods is the officially recommended method, why not use the following method? This is because the relative path method can only be used inside a package. And dart officially does not recommend placing lib in the reference path. If you want to refer to files inside lib, you must use package:.

Of course, if it is a reference inside the package, the relative path is preferred, for example:

my_package
└─ lib
   ├─ src
   │  └─ stuff.dart
   │  └─ utils.dart
   └─ api.dart
   test
   │─ api_test.dart
   └─ test_utils.dart

Then for lib/api.dart, you can refer to it like this:

import 'src/stuff.dart';
import 'src/utils.dart';

For utils.dart, it can be referenced like this:

import '../api.dart';
import 'stuff.dart';

For test/api_test.dart, it can be referenced like this:

import 'package:my_package/api.dart'; 

import 'test_utils.dart';

In short, do not appear lib in the path of import.

Summarize

The above is the best practice for library writing in dart.

This article has been included in http://www.flydean.com/28-dart-libraries-effective/

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

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


flydean
890 声望437 粉丝

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