当前位置:网站首页>Talk about DART's null safety feature
Talk about DART's null safety feature
2022-07-07 23:01:00 【InfoQ】
Preface
null safety
null safety
null safety
Nullable and non-nullable type
null satety
String
String
null
?
String?
null
String? str1;
String str2;
// OK
str1 = null;
// Report errors
str2 = null;
// OK
List<String?> strList1 = ['a', null, 'c'];
// Report errors
List<String> strList2 = ['a', null, 'c'];
Null assertion operator !
valiable!.xx
nullable
null
!
int? couldReturnNullButDoesnt() => -3;
void main() {
int? nullableInt = 1;
List<int?> intListHasNull = [2, null, 4];
int a = nullableInt!;
int b = intListHasNull.first!;
int c = couldReturnNullButDoesnt()!.abs();
print('a is $a.');
print('b is $b.');
print('c is $c.');
}
Type promotion (Type promotion)
String? str;
if (str != null) {
print(str); // It has been ensured that it is not empty , No compilation errors
}
late keyword
late
late
- This variable has not been assigned a value yet ;
- We will assign a value to this variable after ;
- We guarantee that we will assign a value to this variable before using it .
class Meal {
late String _decription; // Error declaration
set description(String desc) {
_description = 'Meal Description: $desc';
}
String get description => _description;
}
void main() {
final myMeal = Meal();
myMeal.description = 'Feijoada';
print(myMeal.description);
}
late
late
if
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(' Get it done !');
}
Upgrade and modify
- Class properties : The non empty class attribute needs to be determined by the initial value by default , If the class attribute is initialized in another method , Then you can add
late
keyword , Indicates that the property will be initialized later , And it's not empty . If the property may be empty , Then add?
Empty sign . This nullable attribute needs special attention when used , You need to check whether it is empty before you can use , Or usevariable?.xx
Access... In this form , If an explicit attribute has a value , You need to use ! Force specified to be non empty , Such asvariable!.xx
.
- Method parameter : Set whether the parameter can be null or required as required , For mandatory parameters, add... Before the parameter declaration
required
keyword , Can be empty plus?
identification .
- Return value : If the return value may be
null
, Just add... After the return parameter?
identification . If an object in the collection object is empty , Then you need to add... After the type of the collection ? identification , for exampleList<int?>
.
- Will rely on the lowest Dart The version is revised to 2.12.0
environment:
sdk: ">=2.12.0 <3.0.0"
- Modify some third-party plug-in dependencies , Upgrade to support null safety edition , For details, please refer to pub Version Description on .
Dio Step on the pit
Dio
DioError [DioErrorType.other]: type 'Null' is not a subtype of type 'Object'
issue
Dio
CookieManager
headers
Cookie
_cookie
null
_cookie
Cookie
// CookieManager Previous code ,_cookie May be null Lead to Dio The abnormal
void onRequest(
RequestOptions options,
RequestInterceptorHandler handler,
) {
options.headers['Cookie'] = _cookie;
return super.onRequest(options, handler);
}
// After modification
@override
void onRequest(
RequestOptions options,
RequestInterceptorHandler handler,
) {
// null safety You can set... Only if it is not empty
if (_cookie != null) {
options.headers['Cookie'] = _cookie;
}
return super.onRequest(options, handler);
}
summary
null safety
null safety
Dart
dynamic
RequestOptions options
headers
Map<String, dynamic>
null
dynamic
null
边栏推荐
- Debezium series: set role statement supporting mysql8
- 一次搞明白 Session、Cookie、Token,面试问题全稿定
- Sword finger offer 55 - I. depth of binary tree
- It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen
- Software evaluation center ▏ what are the basic processes and precautions for automated testing?
- Debezium系列之:源码阅读之BinlogReader
- 苹果在iOS 16中通过'虚拟卡'安全功能进一步进军金融领域
- Unity local coordinates and world coordinates
- Build an "immune" barrier in the cloud to prepare your data
- UWA Q & a collection
猜你喜欢
数字化转型:五个步骤推动企业进步
Two minutes, talk about some wrong understandings of MySQL index
Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades-KDD2020
安踏DTC | 安踏转型,构建不只有FILA的增长飞轮
Leetcode94. Middle order traversal of binary trees
Nx10.0 installation tutorial
[problem] pytorch installation
Sword finger offer 28 Symmetric binary tree
Personal statement of testers from Shuangfei large factory: is education important for testers?
Gazebo import the mapping model created by blender
随机推荐
0-5vac to 4-20mA AC current isolated transmitter / conversion module
LeetCode142. Circular linked list II [two pointers, two methods for judging links in the linked list and finding ring points]
Explain in detail the communication mode between arm A7 and risc-v e907 on Quanzhi v853
What does the model number of asemi rectifier bridge kbpc1510 represent
Ligne - raisonnement graphique - 4 - classe de lettres
微生物健康网,如何恢复微生物群落
IP network active evaluation system -- x-vision
There is another problem just online... Warm
关于海康ipc的几个参数
微服务架构开源框架详情介绍
Line test - graphic reasoning -5- one stroke class
Leetcode19. Delete the penultimate node of the linked list [double pointer]
Line test - graphic reasoning - 6 - similar graphic classes
【测试面试题】页面很卡的原因分析及解决方案
Form组件常用校验规则-2(持续更新中~)
Why is network i/o blocked?
Online interview, how to better express yourself? In this way, the passing rate will be increased by 50%~
CTF练习
数字藏品加速出圈,MarsNFT助力多元化文旅经济!
Personal statement of testers from Shuangfei large factory: is education important for testers?