参考
涉及数据,都要考虑一下是否可以为空,为空的时候显示什么
可空(?)类型,数据是可以为空的,在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(),