当前位置:网站首页>Dart: about Libraries
Dart: about Libraries
2022-07-03 12:01:00 【J_ D_ Chi】
List of articles
Write it at the front
Dart There are some concepts and usages about libraries .
Content
stay Dart There are three kinds of libraries :
- Custom library
- System libraries
- Third party Library
Custom library
Custom library is the library we write ourselves , stay Dart in , Every Dart Files are all a library by default , Just didn't use library
Keyword to declare .
For example, the following two are equivalent :
library hello;
class Hello{}
---
class Hello{}
So now we know everyone Dart In fact, there is a concept of a library hidden in the document .
We know that Dart Use in _
( Underline ) To represent private , So in one Dart In the file, we use a class _
, It means that this class can only be accessed in the Library :
In the following example _Hi
Class cannot be accessed by other files .
library hello;
class Hello{}
class _Hi {}
---
class Hello{}
class _Hi {}
Generally, it is recommended to ignore library
keyword , Unless you want to generate for your library API file (Dart One of them is called dartdoc Tools for , Used to generate API file ).
When importing the library, you use import 'package:xxx.dart';
System libraries
When importing the library, you use import 'dart:xxx';
One of them is called core The library of ,import 'dart:core';
There is no need to write this sentence , By default, it introduces . This is also when we use some system classes , Such as DateTime
The reason why it is not necessary to import and stock in .
stay Core libraries It's listed in the list Dart Relevant core libraries provided .
Third party Library
stay pubspec.yaml
Introduce a third-party library .pubspec.yaml
You can check the relevant contents in The pubspec file
When importing the library, you use import 'package:xxx.dart';
Part of the library is introduced
We can partially introduce the contents of the Library :
- Include introduction (show)
- Exclude the introduction of (hide)
show
stay hello.dart Declare two classes in .
// hello.dart
class Hello {}
class Hi {}
When introducing, we do the following :
// main.dart
import 'hello.dart' show Hello;
void main() {
Hello();
// Report errors The function 'Hi' isn't defined.
Hi();
}
hide
Take the example above , If it is changed to the following example , The effect is the same :
// main.dart
import 'hello.dart' hide Hi;
void main() {
Hello();
// Report errors The function 'Hi' isn't defined.
Hi();
}
Specify the naming prefix of the Library
When referencing libraries , Sometimes there will be some conflict of names ( For example, there are the same named functions or classes in different files ), Or sometimes we want to change the name of this library in a file for some reason .
You can use as To give the library an alias , Take this alias when using .
import 'hello.dart' as h;
void main() {
h.Hello();
h.Hi();
}
Delayed introduction ( Lazy loading )
Dart It allows us to delay the import and storage , It uses deferred as
.
import 'hello.dart' deferred as h;
void main() {
// The direct call will report an error
h.Hello();
h.Hi();
}
Due to the use of deferred as
, When we want it to work , You should call it first loadLibrary()
Method , This is an asynchronous method , therefore :
import 'hello.dart' deferred as h;
void main() {
greet();
}
Future greet() async {
await h.loadLibrary();
h.Hello();
h.Hi();
}
Officials have suggested some scenarios for using lazy load libraries :
- Want to reduce Web app Initialization start time of
- Want to be in A/B Try to replace the implementation of the algorithm in the test
- Want to load some rarely used methods , For example, pop-up window
loadLibrary()
Method we can call multiple times , But in fact, it only loads once . Because it is lazy to load , So if constants are defined in the Library , It also needs to be loaded before it can be used .
Part of the library is introduced
stay Dart We use part
and part of
To split the Library .
For example, there is a main library , And multiple sub libraries , In this way, we can let different people take charge of a library , Finally, put them into a main library :
// sub2_lib.dart
// Establish contact with the main library
part of main_lib;
class Sub2Lib {
Sub2Lib() {
print("Sub2Lib");
}
}
// sub1_lib.dart
part of main_lib;
class Sub1Lib {
Sub1Lib() {
print("Sub1Lib");
}
}
// main_lib.dart
// The name of the main database should be consistent with that of the sub database
library main_lib;
// Import sub Library
part 'sub1_lib.dart';
part 'sub2_lib.dart';
// main.dart
import 'main_lib.dart';
void main() {
Sub1Lib();
Sub2Lib();
}
Reference resources
边栏推荐
猜你喜欢
Is BigDecimal safe to calculate the amount? Look at these five pits~~
rxjs Observable filter Operator 的实现原理介绍
Vulnhub narak
Introduction to the implementation principle of rxjs observable filter operator
"Jianzhi offer 04" two-dimensional array search
Raven2 of vulnhub
AOSP ~ NTP (Network Time Protocol)
shardingSphere分库分表<3>
Download address and installation tutorial of vs2015
《剑指offer 03》数组中重复的数字
随机推荐
(数据库提权——Redis)Redis未授权访问漏洞总结
"Jianzhi offer 04" two-dimensional array search
vulnhub之Nagini
外插散点数据
错排问题 (抽奖,发邮件)
Solutions to the failure of installing electron
The tutor put forward 20 pieces of advice to help graduate students successfully complete their studies: first, don't plan to take a vacation
QT OpenGL rotate, pan, zoom
R language uses the aggregate function to calculate the mean value (sum) of dataframe data grouping aggregation without setting na The result of RM calculation. If the group contains the missing value
Mysql根据时间搜索常用方法整理
Socket TCP for network communication (I)
PHP export word method (one MHT)
Uniapp implementation Click to load more
Deploying WordPress instance tutorial under coreos
Wrong arrangement (lottery, email)
Oracle advanced (I) realize DMP by expdp impdp command
[MySQL special] read lock and write lock
Vulnhub narak
【mysql官方文档】死锁
Vulnhub's cereal