当前位置:网站首页>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(),
边栏推荐
- 十二、form表单的提交
- No code development platform data ID introductory tutorial
- vant-swipe adaptive picture height + picture preview
- 2022 China Eye Expo, Shandong Eye Health Exhibition, Vision Correction Instrument Exhibition, Eye Care Products Exhibition
- 服务间歇性停顿问题优化|得物技术
- 【斯坦福计网CS144项目】Lab5: NetworkInterface
- WebShell 木马免杀过WAF
- 主流定时任务解决方案全横评
- RollBack Rx Professional RMC 安装教程
- 年近30 ,4月无情被辞,想给划水的兄弟提个醒...
猜你喜欢
Technology Sharing | How to do assertion verification for xml format in interface automation testing?
用了这么多年的LinkedList,作者说自己从来不用它?为什么?
用大白话解释“什么是ERP?” 看完这篇就全明白了
js基础知识整理之 —— 五种输出方式
# DWD层及DIM层构建## ,220801 ,
vscode 自定义快捷键——设置eslint
年近30 ,4月无情被辞,想给划水的兄弟提个醒...
即席查询—— Kylin使用
【斯坦福计网CS144项目】Lab5: NetworkInterface
Controller层代码这么写,简洁又优雅!
随机推荐
IDEA 重复代码的黄色波浪线取消设置
Teach you to locate online MySQL slow query problem hand by hand, package teaching package meeting
精心整理16条MySQL使用规范,减少80%问题,推荐分享给团队
HVV红队 | 渗透测试思路整理
Day117.尚医通:生成挂号订单模块
js基础知识整理之 —— Math
D with json
B站回应HR称用户是Loser:涉事面试官去年底已被劝退
简单聊聊MySQL中的六种日志
vscode 自定义快捷键——设置eslint
js基础知识整理之 —— 闭包
ORA-55610: Invalid DDL statement on history-tracked table
2022中国眼博会,山东眼健康展,视力矫正仪器展,护眼产品展
4、Citrix MCS云桌面无法安装todesk等软件
解决错误:Optional int parameter ‘pageSize‘ is present but cannot be translated into a null value due to
【斯坦福计网CS144项目】Lab5: NetworkInterface
js基础知识整理之 —— Date和定时器
nmap: Bad CPU type in executable
Week 7 CNN Architectures - LeNet-5、AlexNet、VGGNet、GoogLeNet、ResNet
十年架构五年生活-04第一个工作转折点