当前位置:网站首页>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
边栏推荐
- Dynamically monitor disk i/o with ZABBIX
- (database authorization - redis) summary of unauthorized access vulnerabilities in redis
- DEJA_VU3D - Cesium功能集 之 053-地下模式效果
- How to convert a numeric string to an integer
- Interview experience in summer camp of Central South University in 2022
- mysql使用update联表更新的方法
- STL tutorial 10 container commonalities and usage scenarios
- 优化接口性能
- OpenGL 着色器使用
- OpenGL 绘制彩色的三角形
猜你喜欢
随机推荐
After watching the video, AI model learned to play my world: cutting trees, making boxes, making stone picks, everything is good
MySQL union和union all区别
shardingSphere分库分表<3>
Concurrent programming - singleton
Raven2 of vulnhub
The tutor put forward 20 pieces of advice to help graduate students successfully complete their studies: first, don't plan to take a vacation
DNS multi-point deployment IP anycast+bgp actual combat analysis
Pragma pack syntax and usage
previous permutation lintcode51
错排问题 (抽奖,发邮件)
Vulnhub's Tomato (tomato)
vulnhub之momentum
laravel 时区问题timezone
vulnhub之tomato(西红柿)
vulnhub之GeminiInc
Colleagues wrote a responsibility chain model, with countless bugs
Mysql根据时间搜索常用方法整理
How to convert a numeric string to an integer
vulnhub之Nagini
typeScript







![[learning notes] DP status and transfer](/img/5e/59c64d2fe08b89fba2d7e1e6de2761.png)

