简体   繁体   English

我在 flutter 中遇到 null 安全问题

[英]I have a problem with null safety in flutter

I'm having a problem with null safety in Flutter, I want to put my data inside horizontal_data.table in Flutter, and I call it with StreamBuilder but when I call it inside the widget, it shows the error:我在 Flutter 中遇到 null 安全问题,我想将我的数据放在Flutter中的 horizontal_data.table 中,我用StreamBuilder调用它,但是当我在小部件中调用它时,它显示错误:

The property 'paidLeaveTypeName' can't be unconditionally accessed because the receiver can be 'null'.无法无条件访问属性“paidLeaveTypeName”,因为接收者可以为“null”。 Try making the access conditional (using '?.') or adding a null check to the target ('.').尝试使访问有条件(使用“?.”)或向目标(“.”)添加 null 检查。

and this is the full code of the Column itself:这是Column本身的完整代码:

Widget _generateRightHandSideColumnRow(BuildContext context, int index) {
    return Row(
      children: <Widget>[
        Container(
          width: 200,
          height: 52,
          padding: const EdgeInsets.fromLTRB(5, 0, 0, 0),
          alignment: Alignment.centerLeft,
          child: StreamBuilder<dynamic>(
            stream: _leavelistbloc.DataStream,
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                switch (snapshot.data!.status!) {
                  case Status.INITIAL:
                    return Text('');
                  case Status.LOADING:
                    return Text('');
                  case Status.COMPLETED:
                    LeaveListModel responses =
                        snapshot.data!.data as LeaveListModel;
                    return Text(responses.response.paidLeaveTypeName);
                  case Status.ERROR:
                    return Text('');
                }
              }
              return Container();
            },
          ),
        ),
      ],
    );
  }
}

and this is exactly where the error code:这正是错误代码所在的位置:

                case Status.COMPLETED:
                    LeaveListModel responses =
                        snapshot.data!.data as LeaveListModel;
                    return Text(responses.response.paidLeaveTypeName);

the model data of the result is look like this:结果的 model 数据如下所示:

class LeaveListResult {
  int? paidLeaveId;
  String? paidLeaveUuid;
  String? paidLeaveTypeName;
  String? paidLeaveEmployeeUuid;
  String? paidLeaveEmployeeNip;
  String? paidLeaveEmployeeFullName;
}

but in case you're wondering what the full code of model looks like, you can see it on here is anyone know how to deal with this or how to call the data inside horizontal_data.table with using StreamBuilder ?但是如果您想知道 model 的完整代码是什么样的,您可以在此处查看是否有人知道如何处理此问题或如何使用StreamBuilder调用 horizontal_data.table 中的数据?

Try this way by checking null response通过检查 null 响应来尝试这种方式

  final result  = responses.response?.result;
   if(result==null) Text("got null result");
   else if (result!.length > 0) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM