当前位置:网站首页>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

边栏推荐
- 行测-图形推理-9-线条问题类
- There is another problem just online... Warm
- Line test - graphic reasoning - 6 - similar graphic classes
- Digital transformation: five steps to promote enterprise progress
- Sword finger offer 27 Image of binary tree
- 行测-图形推理-2-黑白格类
- What is ADC sampling rate (Hz) and how to calculate it
- Unity与WebGL的相爱相杀
- 软件测评中心▏自动化测试有哪些基本流程和注意事项?
- 数据库每日一题---第22天:最后一次登录
猜你喜欢
Early childhood education industry of "screwing bar": trillion market, difficult to be a giant
Apple further entered the financial sector through the 'virtual card' security function in IOS 16
Leetcode19. Delete the penultimate node of the linked list [double pointer]
行测-图形推理-1-汉字类
Robot autonomous exploration series papers environment code
Micro service remote debug, nocalhost + rainbow micro service development second bullet
CTF练习
Line test - graphic reasoning - 6 - similar graphic classes
消息队列与快递柜之间妙不可言的关系
Form组件常用校验规则-2(持续更新中~)
随机推荐
Microservice Remote debug, nocalhost + rainbond microservice Development second Bomb
Leetcode206. Reverse linked list
PHP records the pitfalls encountered in the complete docking of Tencent cloud live broadcast and im live group chat
Early childhood education industry of "screwing bar": trillion market, difficult to be a giant
微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
The PHP source code of the new website + remove authorization / support burning goose instead of pumping
行测-图形推理-3-对称图形类
Knowledge drop - PCB manufacturing process flow
Two minutes, talk about some wrong understandings of MySQL index
肠道里的微生物和皮肤上的一样吗?
Yarn cannot view the historical task log of yarn after enabling ACL user authentication. Solution
Line test - graphic reasoning - 1 - Chinese character class
Unity and webgl love each other
消费品企业敏捷创新转型案例
PHP method of obtaining image information
LeetCode206. Reverse linked list [double pointer and recursion]
Amesim2016 and matlab2017b joint simulation environment construction
Ligne - raisonnement graphique - 4 - classe de lettres
7-18 simple simulation of banking business queue
Qt Graphicsview图形视图使用总结附流程图开发案例雏形