Flutter/Dart - PageViewBuilder 提供的数据的空检查错误
Flutter/Dart - Null Check Error with data provided by PageViewBuilder
我在 PageViewBuilder
;
提供的数据上收到以下空检查错误
The following _CastError was thrown building:
Null check operator used on a null value
When the exception was thrown, this was the stack:
#0 _StageBuilderState.createViewItem (package:speakoholic/screens/mainStage.dart:488:161)
#1 _StageBuilderState.build.<anonymous closure> (package:speakoholic/screens/mainStage.dart:138:18)
488行是speakcraft.tagname!
调用这个函数时;
child: editIcon(
context: context,
userid:speakcraft.userid!,
audioid:speakcraft.audioid!,
title: speakcraft.title!,
tagname:speakcraft.tagname!),
而第 138 行是 createViewItem
PageView.builder(
itemCount: widget.speakcrafts.length,
itemBuilder: (context, int currentIndex) {
return createViewItem(widget.speakcrafts[currentIndex], context, currentIndex, widget.speakcrafts.length );
},
);
这是函数;
Widget editIcon({
required BuildContext? context,
required int? userid,
required int? audioid,
required String? title,
required String? tagname}) {
return
FutureBuilder<int?>(
///some stuff
);
}
如何解决这些 nullcheck 错误?
问题是您试图通过 Bang !
运算符(也称为 null 断言运算符)来使用不可为 null 的值来消除可空性:tagname:speakcraft.tagname!
.
另一方面,您的 属性 可以为空,因为 String? tagname
意味着 tagname
可以是 null
。
因此,为了避免这种情况,您可以更改 editIcon
方法:
editIcon(
...
tagname:speakcraft?.tagname ?? 'some default value'),
我在 PageViewBuilder
;
The following _CastError was thrown building:
Null check operator used on a null value
When the exception was thrown, this was the stack:
#0 _StageBuilderState.createViewItem (package:speakoholic/screens/mainStage.dart:488:161)
#1 _StageBuilderState.build.<anonymous closure> (package:speakoholic/screens/mainStage.dart:138:18)
488行是speakcraft.tagname!
调用这个函数时;
child: editIcon(
context: context,
userid:speakcraft.userid!,
audioid:speakcraft.audioid!,
title: speakcraft.title!,
tagname:speakcraft.tagname!),
而第 138 行是 createViewItem
PageView.builder(
itemCount: widget.speakcrafts.length,
itemBuilder: (context, int currentIndex) {
return createViewItem(widget.speakcrafts[currentIndex], context, currentIndex, widget.speakcrafts.length );
},
);
这是函数;
Widget editIcon({
required BuildContext? context,
required int? userid,
required int? audioid,
required String? title,
required String? tagname}) {
return
FutureBuilder<int?>(
///some stuff
);
}
如何解决这些 nullcheck 错误?
问题是您试图通过 Bang !
运算符(也称为 null 断言运算符)来使用不可为 null 的值来消除可空性:tagname:speakcraft.tagname!
.
另一方面,您的 属性 可以为空,因为 String? tagname
意味着 tagname
可以是 null
。
因此,为了避免这种情况,您可以更改 editIcon
方法:
editIcon(
...
tagname:speakcraft?.tagname ?? 'some default value'),