当前位置:网站首页>Dart series: look at me for security. The security feature in dart is null safety
Dart series: look at me for security. The security feature in dart is null safety
2022-06-23 21:38:00 【Procedural stuff】
brief introduction
stay Dart 2.12 Introduced in null safety New features , in other words dart The default types in the program are non empty , Unless you tell the compiler , This type can be empty .
It looks like a small change , But this small change has led to a lot Dart Large version upgrade of package , This leads to the use of Dart2.12 Previous versions follow dart2.12 The later version is completely two different worlds .
Is it really so wonderful ? Let's see Dart 2.12 null safety Characteristics of .
Non-nullable type
In feeling Non-nullable Before the type , Let's take a look at a piece of code :
void main(){
String name;
print('name is $name.');
}The code is simple , We define a String Type of name character string , And then in print Print it in the statement .
If you are dart 2.12 Before the release , Then there is no problem .
But here we are 2.12, May be an error :
The non-nullable local variable 'name' must be assigned before it can be used. Try giving it an initializer expression, or ensure that it's assigned on every execution path.
That is to say ,name It's not empty , You have to give him a value .
By forcing not to be empty , Ensure the security of the code , Very easy to use .
So if name If it can be empty, how to deal with it ?
Don't worry. , We can add... After the type that can be empty ? that will do :
void main(){
String? name;
print('name is $name.');
}Nullable List Of Strings and List Of Nullable Strings
If we're going to create a List,list It contains String, You can create :
List<String> aListOfStrings = ['one', 'two', 'three'];
stay dart 2.12 in , Non null checking is also used in generics , therefore , By default List Medium String It can't be empty , If it has to be empty , It needs to be written like this :
List<String?> aListOfNullableStrings = ['one', null, 'three'];
about list itself , It can't be empty , If empty , It needs to be written like this :
List<String>? aNullableListOfStrings;
! The operator
If you think an object is not when it is used null, Then you can add... After the expression !, As shown below :
String? word; word = aListOfNullableStrings.first; print(word!.length);
late keyword
occasionally , We know that an object must not be empty , But for now , It cannot be assigned immediately , You need to use late keyword .
Here's an example that uses late Example :
class Team {
late final Coach coach;
}
class Coach {
late final Team team;
}
void main() {
final myTeam = Team();
final myCoach = Coach();
myTeam.coach = myCoach;
myCoach.team = myTeam;
print('All done!');
}In the above code , We have two classes that refer to each other , But both classes are not empty , If not used late, Will fail to compile .
Use late You can initialize the properties in the class at an appropriate time later , So as to ensure the operation of the code .
summary
That's all dart 2.12 The new null safety Use .
This article has been included in http://www.flydean.com/24-dart-null-safety/
边栏推荐
- Thinking about distributed system consensus
- What is the process of opening a mobile card account? Is online account opening safe?
- How to download offline versions of Firefox and chrome
- Aiot application innovation competition - get better code completion and jump experience with clion
- 【Proteus仿真】LCD1602+DS1307按键设置简易时钟
- What can RFID fixed assets management system bring to enterprises?
- Talk about how to customize data desensitization
- Troubleshooting the problem that the channel cannot play after easycvr cascades to the upper platform
- 数字电路概述
- What is a database index? Xinhua dictionary to help you
猜你喜欢

Selenium batch query athletes' technical grades

CAD图在线Web测量工具代码实现(测量距离、面积、角度等)

Beitong G3 game console unpacking experience. It turns out that mobile game experts have achieved this

What are the main dimensions of PMO performance appraisal?

New SQL syntax quick manual!
![Harmonyos application development -- mynotepad[memo][api v6] based on textfield and image pseudo rich text](/img/b1/71cc36c45102bdb9c06e099eb42267.jpg)
Harmonyos application development -- mynotepad[memo][api v6] based on textfield and image pseudo rich text

Minimisé lorsque Outlook est allumé + éteint

Facing the problem of lock waiting, how to realize the second level positioning and analysis of data warehouse

How PMO uses two dimensions for performance appraisal

微信小程序中发送网络请求
随机推荐
手机卡开户的流程是什么?在线开户安全么?
Thinking about distributed system consensus
Coding website hosting migration Tencent cloud cloud development webify
Outlook開機自啟+關閉時最小化
Who do you want to open a stock account? Is it safe to open an account online?
智能座舱SoC竞争升级,国产7nm芯片迎来重要突破
New SQL syntax quick manual!
SAP Migo mobile type 311 attempts to determine the batch, and the system reports an error -batch determination not Po
【Debian】Debian使用笔记
Bi-sql index
Minimize outlook startup + shutdown
[tutorial] Tencent lightweight cloud builds an online customer service chat system
How PMO uses two dimensions for performance appraisal
How to write test cases efficiently?
Connect edgex gateway to thingsboard IOT platform
Do you really understand the cache penetration, cache breakdown and cache avalanche in rotten street?
[JS 100 examples of reverse] anti climbing practice platform for net Luozhe question 5: console anti debugging
What are the main dimensions of PMO performance appraisal?
Open source C # WPF control library --newbeecoder UI usage guide (III)
Use of pathinfo/pathname in nodejs