当前位置:网站首页>flutter空安全问题,平时用到的数据一定要注意
flutter空安全问题,平时用到的数据一定要注意
2022-08-02 23:19:00 【氤氲息】
参考
涉及数据,都要考虑一下是否可以为空,为空的时候显示什么
可空(?)类型,数据是可以为空的,在json解析的范类里面基本是?,因为服务器可能会返回空值
class BottleModel {
BottleModel({
String? appId,
int? downTime,
int? downUserId,
int? driftBottleId,
int? level,
int? noReadNum,
int? status,
String? text,
int? upTime,
int? upUserId,
}) {
_appId = appId;
_downTime = downTime;
_downUserId = downUserId;
_driftBottleId = driftBottleId;
_level = level;
_noReadNum = noReadNum;
_status = status;
_text = text;
_upTime = upTime;
_upUserId = upUserId;
}
BottleModel.fromJson(dynamic json) {
_appId = json['app_id'];
_downTime = json['down_time'];
_downUserId = json['down_user_id'];
_driftBottleId = json['drift_bottle_id'];
_level = json['level'];
_noReadNum = json['no_read_num'];
_status = json['status'];
_text = json['text'];
_upTime = json['up_time'];
_upUserId = json['up_user_id'];
}
String? _appId;
int? _downTime;
int? _downUserId;
int? _driftBottleId;
int? _level;
int? _noReadNum;
int? _status;
String? _text;
int? _upTime;
int? _upUserId;
BottleModel copyWith({
String? appId,
int? downTime,
int? downUserId,
int? driftBottleId,
int? level,
int? noReadNum,
int? status,
String? text,
int? upTime,
int? upUserId,
}) =>
BottleModel(
appId: appId ?? _appId,
downTime: downTime ?? _downTime,
downUserId: downUserId ?? _downUserId,
driftBottleId: driftBottleId ?? _driftBottleId,
level: level ?? _level,
noReadNum: noReadNum ?? _noReadNum,
status: status ?? _status,
text: text ?? _text,
upTime: upTime ?? _upTime,
upUserId: upUserId ?? _upUserId,
);
String? get appId => _appId;
int? get downTime => _downTime;
int? get downUserId => _downUserId;
int? get driftBottleId => _driftBottleId;
int? get level => _level;
int? get noReadNum => _noReadNum;
int? get status => _status;
String? get text => _text;
int? get upTime => _upTime;
int? get upUserId => _upUserId;
Map<String, dynamic> toJson() {
final map = <String, dynamic>{
};
map['app_id'] = _appId;
map['down_time'] = _downTime;
map['down_user_id'] = _downUserId;
map['drift_bottle_id'] = _driftBottleId;
map['level'] = _level;
map['no_read_num'] = _noReadNum;
map['status'] = _status;
map['text'] = _text;
map['up_time'] = _upTime;
map['up_user_id'] = _upUserId;
return map;
}
}
class Person{
String name = "Tony"
}
var userName ;
late Person p = Person();
userName = p?.name;
print(userName)
userName = p?.name; 等同于
if(p == null){
userName = null;//就是可空的意思
}else {
userName = p.name;
}
?? 如果是空的就赋后面那个值
var expr1;
var expr2 = 'b';
expr1 = expr1 ?? expr2;
如果expr1为null,那么将expr2赋值给expr1, 反之如果不为null,就将expr1赋值给expr1.等同于:
expr1 ??= expr2;
等同于:
if(expr1 == null){
expr1 = expr2
}
延迟初始化(late)
可以不使用?.,可以让开发者自己选择初始化的时机,比如在initState进行初始化
空值断言操作符(!)
赋!的一定是不能为null
处理方式:
String getName() {
String name = "";//如果为空值,保底赋值为“”
if (replyList != null) {
if (replyList!.otherInfo != null) {
if (replyList!.otherInfo!.nickname != null) {
//全部都不为空的时候才赋值
name = replyList!.otherInfo!.nickname!;
}
}
}
return name;
}
if (getName() != "")//如果不是“”,下面的内容才可以显示
UserExInfoWidget(
nickName: getName(),//调用方法,在方法里面判空
nickFontSize: 17.0,
nickFontColor: CommonColors.getColor1A1A1A,
),
如果是列表,就判断是否为空,如果不为空才可以正常显示,如果是空的就返回一个Container
(currentMessageList == null || currentMessageList.length == 0)
? [Container()]: replyList == null
? [Container()]
: currentMessageList.map((e) => MsgContainer(replyList: replyList!,downList: e,)).toList(),
边栏推荐
- 2022 China Eye Expo, Shandong Eye Health Exhibition, Vision Correction Instrument Exhibition, Eye Care Products Exhibition
- nmap: Bad CPU type in executable
- 合并两个excel表格工具
- threejs 动态调整相机位置,使相机正好能看到对象
- 定了!8月起,网易将为本号粉丝提供数据分析培训,费用全免!
- threejs dynamically adjust the camera position so that the camera can see the object exactly
- Connect the Snowflake of CKAN tutorial CKAN to release to open data portal
- WAF WebShell Trojan free to kill
- 非关系型数据库MongoDB简介和部署
- CKAN教程之在 AWS 上部署 CKAN 应用程序
猜你喜欢

程序员如何优雅地解决线上问题?

js基础知识整理之 —— 变量和数据类型

高数---二重积分

Technology Sharing | How to do assertion verification for xml format in interface automation testing?

聚乙二醇衍生物4-Arm PEG-DSPE,四臂-聚乙二醇-磷脂

What is the matter that programmers often say "the left hand is knuckled and the right hand is hot"?

智能电视竞争白热化,利用小程序共建生态突围

MySQL最大建议行数2000w, 靠谱吗?

用大白话解释“什么是ERP?” 看完这篇就全明白了

如何突破测试/开发程序员思维?一种不一样的感觉......
随机推荐
d合并json
买母婴产品先来京东“券民空间站”抢券!大牌好物低至5折
markdown语法
科研用Cholesterol-PEG-NHS,NHS-PEG-CLS,胆固醇-聚乙二醇-活性酯
vscode 自定义快捷键——设置eslint
VMware workstation 程序启动慢
Teach you to locate online MySQL slow query problem hand by hand, package teaching package meeting
IDEA 重复代码的黄色波浪线取消设置
典型相关分析CCA计算过程
数据库主键一定要自增吗?有哪些场景不建议自增?
精心整理16条MySQL使用规范,减少80%问题,推荐分享给团队
Jmeter二次开发实现rsa加密
js基础知识整理之 —— 闭包
5、Citrix云桌面初始化Storefront设置
程序员如何优雅地解决线上问题?
CTF命令执行题目解题思路
Merge two excel spreadsheet tools
为了面试阿里,熬夜肝完这份软件测试笔记后,Offer终于到手了
服务间歇性停顿问题优化|得物技术
用大白话解释“什么是ERP?” 看完这篇就全明白了