![](/img/trans.png)
[英]How to fix "null check operator used on a null value" in flutter?
[英]How to fix " Null check operator used on a null value" error ? (Flutter)
我的代码出现了一些错误,我无法弄清楚如何准确修复它。 每次我点击“编辑项目”我都会得到那个错误。 它还告诉我:在构建 FormBuilderField(dirty, dependencies: [_FormScope, UnmanagedRestorationScope], state: FormBuilderFieldState<FormBuilderField, int>#e0024) 时抛出了以下 _CastError: Null check operator used on a null value
相关的导致错误的小部件是:FormBuilderField FormBuilderField:file:///lib/src/widgets/selected_ngo_widget.dart:23:12
这里是选择器的代码:
class SelectedNgo extends StatefulWidget {
int? id;
String? name;
String? logo;
int? defaultValue;
final GlobalKey<FormBuilderState> formKey;
SelectedNgo(this.formKey, {this.defaultValue, this.name, this.logo, this.id, Key? key})
: super(key: key);
@override
State<SelectedNgo> createState() => _SelectedNgoState();
}
class _SelectedNgoState extends State<SelectedNgo> {
@override
Widget build(BuildContext context) {
return FormBuilderField(
name: 'ngoId',
initialValue: widget.id,
builder: (FormFieldState<int> field) {
return Form(
child: Card(
child: Row(
children: [
const SizedBox(
width: 10,
),
Padding(
padding: const EdgeInsets.all(10),
child: CachedNetworkImage(
imageUrl: '${widget.logo}_SMALL.jpg',
placeholder: (context, url) => const CircularProgressIndicator(),
errorWidget: (context, url, error) => const Icon(Icons.error),
height: 80,
fit: BoxFit.contain,
),
),
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
widget.name!,
textAlign: TextAlign.center,
style: GoogleFonts.k2d(
textStyle: const TextStyle(
fontWeight: FontWeight.w600,
fontSize: 14,
color: Colors.black,
),
),
),
],
))
],
),
),
);
});
}
}
预先感谢您的帮助!
这里 widget.name 必须为空。 您可以将其添加到这样的字符串中
Text('${widget.name!}')
但请注意,您没有将名称传递给此小部件。 这将显示 null 代替名称
“用于空值的空检查运算符”意味着您使用!
在一个为null
的变量上。 在显示的代码中,您使用它的唯一地方是
widget.name!
这意味着name
必须为null
。 您必须在某处创建SelectedNgo
而不传递名称或将null
作为名称传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.