我应该如何正确处理代码中的空错误?
How should I properly go about null errors in my code?
我是 Flutter Mobile 应用程序框架的新手,我仍在努力理解 Darts 空安全。
在第 11 行,我不确定如何解决我的代码中的这个错误。
~ 不可为 null 的实例字段 '_numberfrom' 必须初始化。
尝试添加初始化表达式,或初始化它或将其标记为延迟的生成构造函数。
- 我应该将其标记为晚吗?这是初始化 'numberfrom' 的正确方法吗?
- 第 37 行我的代码也收到蓝线
Text((_numberFrom == null) ? '' : _numberFrom.toString()),
~ 操作数不能为空,因此条件始终为假,请尝试删除条件、封闭条件或整个条件语句。
任何帮助将不胜感激谢谢!我也附上了我的代码片段,我真的在寻找想法并帮助正确修复错误。enter image description here
您可以像这样在 Text
小部件中使用 if null
运算符:
Text(_numberFrom.toString() ?? 'default value')
您可以查看有关空安全的更多信息here
如果要检查 null
值,请通过像 int? _numberFrom;
那样声明使其可为空。
然后你可以使用 Text((_numberFrom == null) ? '' : _numberFrom.toString()),
但是,如果您使用 late
声明变量,则会出现异常。
如果你这样做 Text((_numberFrom == null) ? '' : _numberFrom.toString()),
它总是会占用空间。
我更喜欢不想使用默认值 (column/stacks..)
if (_numberFrom != null) Text(_numberFrom.toString()),
国内独生子女使用Visibilty
Visibility(
visible: _numberFrom != null,
child: Text(_numberFrom.toString()),
),
结果
Text((_numberFrom == null) ? '' : _numberFrom.toString()),
它将始终占用空格 RedColor.
测试小工具
class TestCenter extends StatelessWidget {
@override
Widget build(BuildContext context) {
int? _numberFrom;
int? num2;
late int num3;
return Column(
children: [
Container(
width: double.infinity,
height: 100,
color: Colors.deepPurple,
child: Text("Top"),
),
Text((_numberFrom == null) ? '' : _numberFrom.toString()),
Text((num2 == null) ? "num2 is null" : num2.toString()),
///* this will will be cause errors
// Text(
// (num3 == null ? "num3 is null" : num3.toString()),
// ),
///* I prefer when i dont want to use default value
if (_numberFrom != null) Text(_numberFrom.toString()),
Visibility(
visible: _numberFrom != null,
child: Text(_numberFrom.toString()),
),
Container(
width: double.infinity,
height: 100,
color: Colors.deepPurple,
child: Text("Bottom >"),
)
],
);
}
}
我是 Flutter Mobile 应用程序框架的新手,我仍在努力理解 Darts 空安全。 在第 11 行,我不确定如何解决我的代码中的这个错误。
~ 不可为 null 的实例字段 '_numberfrom' 必须初始化。 尝试添加初始化表达式,或初始化它或将其标记为延迟的生成构造函数。
- 我应该将其标记为晚吗?这是初始化 'numberfrom' 的正确方法吗?
- 第 37 行我的代码也收到蓝线
Text((_numberFrom == null) ? '' : _numberFrom.toString()), ~ 操作数不能为空,因此条件始终为假,请尝试删除条件、封闭条件或整个条件语句。
任何帮助将不胜感激谢谢!我也附上了我的代码片段,我真的在寻找想法并帮助正确修复错误。enter image description here
您可以像这样在 Text
小部件中使用 if null
运算符:
Text(_numberFrom.toString() ?? 'default value')
您可以查看有关空安全的更多信息here
如果要检查 null
值,请通过像 int? _numberFrom;
那样声明使其可为空。
然后你可以使用 Text((_numberFrom == null) ? '' : _numberFrom.toString()),
但是,如果您使用 late
声明变量,则会出现异常。
如果你这样做 Text((_numberFrom == null) ? '' : _numberFrom.toString()),
它总是会占用空间。
我更喜欢不想使用默认值 (column/stacks..)
if (_numberFrom != null) Text(_numberFrom.toString()),
国内独生子女使用Visibilty
Visibility(
visible: _numberFrom != null,
child: Text(_numberFrom.toString()),
),
结果
Text((_numberFrom == null) ? '' : _numberFrom.toString()),
它将始终占用空格 RedColor.
测试小工具
class TestCenter extends StatelessWidget {
@override
Widget build(BuildContext context) {
int? _numberFrom;
int? num2;
late int num3;
return Column(
children: [
Container(
width: double.infinity,
height: 100,
color: Colors.deepPurple,
child: Text("Top"),
),
Text((_numberFrom == null) ? '' : _numberFrom.toString()),
Text((num2 == null) ? "num2 is null" : num2.toString()),
///* this will will be cause errors
// Text(
// (num3 == null ? "num3 is null" : num3.toString()),
// ),
///* I prefer when i dont want to use default value
if (_numberFrom != null) Text(_numberFrom.toString()),
Visibility(
visible: _numberFrom != null,
child: Text(_numberFrom.toString()),
),
Container(
width: double.infinity,
height: 100,
color: Colors.deepPurple,
child: Text("Bottom >"),
)
],
);
}
}