Introduction to the
Library is a useful way for DART to organize code, and by defining different libraries, you can encapsulate useful DART code and make it available to other projects. We are free to use import or export to import the library. But what is the most appropriate usage? Take a look.
Use part and part of
While many programmers hate using parts, Dart does provide the ability to split a large lib into smaller files.
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 made up of referenced files.
Dart,student_name.dart, and student.dart.
The first two files are part of the last one.
student_age.dart:
part of student;
int getAge(){
return 18;
}
Copy the code
student_name.dart:
part of student;
String getName(){
return "jack";
}
Copy the code
student.dart:
library student;
part 'some/other/student_age.dart';
part 'some/other/student_name.dart';
Copy the code
What’s wrong with the code above?
The problem with student_age. Dart is that the part of “of” specifies the library to which it belongs, but we don’t know where the library is.
So it should read:
part of '.. /.. /student.dart';Copy the code
Files in SRC
By default, SRC files in the lib directory are only used inside the package and are not allowed to be called by external projects.
So we must not import SRC files directly into the lib package.
Lib files in package
For package, files in lib are exportable, but it is best not to import files in lib directly using absolute or relative paths when importing package.
Instead, use import ‘package:’.
For example, suppose we have a library file with the following structure:
My_package ├ ─ garbage └─ APICopy the code
Api.dart is the file to export. Dart If we need to reference api.dart in api_test.dart, we can do it in one of two ways:
import 'package:my_package/api.dart';
Copy the code
And:
import '.. /lib/api.dart';Copy the code
The above method is the official recommended method, why not use the following method? This is because the relative path approach can only be used inside a package. Dart does not recommend placing lib in the reference path. If you want to reference files inside the lib, use package:.
Of course, relative paths are preferred for references within the package, such as:
My_package └ ─ lib ├ ─ SRC │ └ ─ stuff. The dart │ └ ─ utils. Dart └ ─ API. The dart test │ ─ api_test. Dart └ ─ test_utils. The dartCopy the code
Dart can be referenced as lib/api.dart:
import 'src/stuff.dart';
import 'src/utils.dart';
Copy the code
For utils.dart, you can quote:
import '.. /api.dart'; import 'stuff.dart';Copy the code
For test/ API_test.dart, you can reference it as follows:
import 'package:my_package/api.dart';
import 'test_utils.dart';
Copy the code
In general, do not have lib in the import path.
conclusion
That’s Library writing best practices in DART.
This article is available at www.flydean.com/28-dart-lib…
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!