• flutter空安全问题,平时用到的数据一定要注意


    参考
    涉及数据,都要考虑一下是否可以为空,为空的时候显示什么
    可空(?)类型,数据是可以为空的,在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;
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    ?? 如果是空的就赋后面那个值

    var expr1;
    var expr2 = 'b';
    expr1 = expr1 ?? expr2;
    如果expr1为null,那么将expr2赋值给expr1, 反之如果不为null,就将expr1赋值给expr1.等同于:
    
    expr1 ??= expr2;
    等同于:
    
    if(expr1 == null){
      expr1  = expr2
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    延迟初始化(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,
                      ),
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    如果是列表,就判断是否为空,如果不为空才可以正常显示,如果是空的就返回一个Container

           (currentMessageList == null || currentMessageList.length == 0)
                      ? [Container()]: replyList == null
                      ? [Container()]
                      : currentMessageList.map((e) => MsgContainer(replyList: replyList!,downList: e,)).toList(),
    
    • 1
    • 2
    • 3
    • 4
  • 相关阅读:
    基于Echarts实现可视化数据大屏通用大数据可视化展示平台模板
    七年码农路
    leetcode152 乘积最大子数组
    用Python自制桌面版翻译软件
    Flink docker-compose 单机版 安装教程
    java常用算法面试题,总结到位
    SpringBoot集成Sharding-JDBC实现主从同步
    管程的介绍
    人工智能 笔记1
    elmentui el-select下拉输入不清空已选值
  • 原文地址:https://blog.csdn.net/weixin_44911775/article/details/126118823