当前位置:网站首页>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(),
边栏推荐
猜你喜欢
Merge two excel spreadsheet tools
别再到处乱放配置文件了!我司使用 7 年的这套解决方案,稳的一秕
用了这么多年的LinkedList,作者说自己从来不用它?为什么?
CAS:1445723-73-8,DSPE-PEG-NHS,磷脂-聚乙二醇-活性酯两亲性脂质PEG共轭物
Jmeter secondary development to realize rsa encryption
定了!8月起,网易将为本号粉丝提供数据分析培训,费用全免!
C语言函数详解(2)【函数参数——实际参数(实参)&形式参数(形参)】
IDEA 重复代码的黄色波浪线取消设置
合并两个excel表格工具
Mock工具之Moco使用教程
随机推荐
反弹shell原理与实现
Strict feedback nonlinear systems based on event trigger preset since the immunity of finite time tracking control
测试人生 | 阿里实习 90 天:从实习生的视角谈谈个人成长
MySQL的多表查询(1)
智能电视竞争白热化,利用小程序共建生态突围
新公链时代的跨链安全性解决方案
What is the matter that programmers often say "the left hand is knuckled and the right hand is hot"?
公司招个程序员,34岁以上两年一跳的不要,开出工资以为看错了
Find My技术|智能防丢还得看苹果Find My技术
服务间歇性停顿问题优化|得物技术
同一份数据,Redis为什么要存两次?
Numpy数组中d[True]=1的含义
IDEA 重复代码的黄色波浪线取消设置
Teach you to locate online MySQL slow query problem hand by hand, package teaching package meeting
如何突破测试/开发程序员思维?一种不一样的感觉......
函数:计算组合数
WebShell 木马免杀过WAF
在软件测试行业近20年的我,再来和大家谈谈今日的软件测试
思源笔记 本地存储无使用第三方同步盘,突然打不开文件。
为了面试阿里,熬夜肝完这份软件测试笔记后,Offer终于到手了