我在 Flutter 中遇到空安全问题,我想将数据放入其中水平数据表 https://pub.dev/packages/horizontal_data_table在 Flutter 中,我用StreamBuilder
但是当我在小部件内调用它时,它显示错误:
无法无条件访问属性“paidLeaveTypeName”,因为接收者可以为“null”。
尝试使访问成为有条件的(使用“?.”)或向目标添加空检查(“!”)。
这是完整的代码Column
itself:
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();
},
),
),
],
);
}
}
这正是错误代码的所在:
case Status.COMPLETED:
LeaveListModel responses =
snapshot.data!.data as LeaveListModel;
return Text(responses.response.paidLeaveTypeName);
结果的模型数据如下所示:
class LeaveListResult {
int? paidLeaveId;
String? paidLeaveUuid;
String? paidLeaveTypeName;
String? paidLeaveEmployeeUuid;
String? paidLeaveEmployeeNip;
String? paidLeaveEmployeeFullName;
}
但如果您想知道模型的完整代码是什么样的,您可以在here https://pastecode.io/s/p3e1s9qi有谁知道如何处理这个问题或如何使用horizontal_data_table调用数据StreamBuilder
?