列表索引无空安全检查 - rangeerror(索引):无效值:有效值范围为空:0
我在这里阅读了许多帖子,似乎没有人直接回答我的问题。如果postsets
是notempty
,我可以打电话来构建以水印的照片,并包含特定的图像文件。我还有一个else
语句,用于列表为空或中间有其他文件类型。但是,如果列表为null,我在列表上运行Null-Safety检查遇到问题。 AssetIndex
作为int
传递给小部件。
class ShareMenuWidget extends StatefulWidget {
const ShareMenuWidget({
Key key,
this.assetIndex,
}) : super(key: key);
final int assetIndex;
@override
_ShareMenuWidgetState createState() => _ShareMenuWidgetState();
}
class _ShareMenuWidgetState extends State<ShareMenuWidget> {
@override
void initState() {
super.initState();
相关代码是
RaisedButton(
onPressed: () async {
//this is the line throwing the error if postAssets is empty or null
final assetUrl = containerPostsRecord.postAssets.elementAt(widget.assetIndex);
if (containerPostsRecord.postAssets.isNotEmpty &&
assetUrl.contains('jpg') || assetUrl.contains('png') || assetUrl.contains('gif') ||
assetUrl.contains('jpeg') || assetUrl.contains('webp') || assetUrl.contains('bpm'))
{
final newImage = Function 1
}
else {
final newImage = Function 2
}
},
当我运行此问题时,我会收到以下错误
RangeError (index): Invalid value: Valid value range is empty: 0
,我可能会使用多个If
语句进行此操作,但是无论如何都可以在Asseturl
声明中检查null postsets
列表?
I have read many posts on here and none seem to answer my question directly. I have a call to build a watermarked photo if the postAssets
is notEmpty
and contains specific image files. I also have an else
statement for when the list is empty or has a different file type in there. However, I am having issues with running the null-safety check on the list if the list is null. The assetIndex
is being passed to the widget as an int
.
class ShareMenuWidget extends StatefulWidget {
const ShareMenuWidget({
Key key,
this.assetIndex,
}) : super(key: key);
final int assetIndex;
@override
_ShareMenuWidgetState createState() => _ShareMenuWidgetState();
}
class _ShareMenuWidgetState extends State<ShareMenuWidget> {
@override
void initState() {
super.initState();
Relevant code is
RaisedButton(
onPressed: () async {
//this is the line throwing the error if postAssets is empty or null
final assetUrl = containerPostsRecord.postAssets.elementAt(widget.assetIndex);
if (containerPostsRecord.postAssets.isNotEmpty &&
assetUrl.contains('jpg') || assetUrl.contains('png') || assetUrl.contains('gif') ||
assetUrl.contains('jpeg') || assetUrl.contains('webp') || assetUrl.contains('bpm'))
{
final newImage = Function 1
}
else {
final newImage = Function 2
}
},
When I run this, I get the following error
RangeError (index): Invalid value: Valid value range is empty: 0
I could probably do this with multiple if
statements, but is there anyway to check for null in the assetUrl
declaration within the postAssets
list?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您必须在使用该值之前检查。这样的事情:
I think you have to check before using the value. Something like this:
在启动代码之前,我将在
Postsets
列表上添加一个空检查或空检查。而且,您随时可以在前添加更多检查,例如索引完全不在范围内,即使它不是空列表。从此答案中显示的示例中使用了此信息:
dart null / false /空检查:如何编写此简短?
I would add a null or empty check on the
postAssets
list before you start the code. And you can always add more checks up front, like if the index is within range at all, even when it is not an empty list.Used this from the example shown in this answer:
Dart null / false / empty checking: How to write this shorter?
尝试以下操作:
Try this: