当前位置:网站首页>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/
边栏推荐
- Surprise! Edge computing will replace cloud computing??
- Go language core 36 lectures (go language practice and application 27) -- learning notes
- Gradle asked seven times. You should know that~
- 【Redis】有序集合的交集与并集
- Uncover the secrets of Huawei cloud enterprise redis issue 16: acid'true' transactions beyond open source redis
- The new track of flagship chip is finally clear
- Infrastructure splitting of service splitting
- [redis] intersection and union of ordered sets
- What are the advantages of attaching a virtual machine to a hard disk cloud server
- Global and Chinese market of fly killers 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

Minimisé lorsque Outlook est allumé + éteint

How PMO uses two dimensions for performance appraisal

Find My资讯|苹果可能会推出第二代AirTag,试试伦茨科技Find My方案

How to gradually improve PMO's own ability and management level

Lightweight, dynamic and smooth listening, hero earphone hands-on experience, can really create

How does PMO select and train project managers?

发现一个大佬云集的宝藏硕博社群!

Minimize outlook startup + shutdown

What are the main dimensions of PMO performance appraisal?

Facing the problem of lock waiting, how to realize the second level positioning and analysis of data warehouse
随机推荐
There is a 1GB difference between truncatememory and removememory
Wechat is new. You can create applications from Excel
Cloud database smooth disassembly scheme
Those programmers wrote super funny 56 code comments (worth collecting)!!
Framework not well mastered? Byte technology Daniel refined analysis notes take you to learn systematically
Coding website hosting migration Tencent cloud cloud development webify
What causes the applet SSL certificate to expire? How to solve the problem when the applet SSL certificate expires?
Game security - call analysis - write code
Analysis of visual analysis technology
Do you really understand the cache penetration, cache breakdown and cache avalanche in rotten street?
Troubleshooting the problem that the channel cannot play after easycvr cascades to the upper platform
Analysis of a series a e-commerce app docommandnative
Initial experience of nodejs express framework
手机卡开户的流程是什么?在线开户安全么?
How PMO uses two dimensions for performance appraisal
How to gradually improve PMO's own ability and management level
Aiot application innovation competition - get better code completion and jump experience with clion
Beitong G3 game console unpacking experience. It turns out that mobile game experts have achieved this
How to download offline versions of Firefox and chrome
同花顺开户是安全的吗?