列表索引无空安全检查 - rangeerror(索引):无效值:有效值范围为空:0

发布于 2025-02-06 07:17:57 字数 1401 浏览 5 评论 0原文

我在这里阅读了许多帖子,似乎没有人直接回答我的问题。如果postsetsnotempty,我可以打电话来构建以水印的照片,并包含特定的图像文件。我还有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梨涡少年 2025-02-13 07:17:58

我认为您必须在使用该值之前检查。这样的事情:

final assetUrl = containerPostsRecord.postAssets.contains(widget.assetIndex) ? containerPostsRecord.postAssets.elementAt(widget.assetIndex) : '';

I think you have to check before using the value. Something like this:

final assetUrl = containerPostsRecord.postAssets.contains(widget.assetIndex) ? containerPostsRecord.postAssets.elementAt(widget.assetIndex) : '';
删除会话 2025-02-13 07:17:58

在启动代码之前,我将在Postsets列表上添加一个空检查或空检查。而且,您随时可以在前添加更多检查,例如索引完全不在范围内,即使它不是空列表。

if (containerPostsRecord.postAssets?.isEmpty ?? true) {
    return; // or throw?
}

// do the rest of your code.

从此答案中显示的示例中使用了此信息:

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.

if (containerPostsRecord.postAssets?.isEmpty ?? true) {
    return; // or throw?
}

// do the rest of your code.

Used this from the example shown in this answer:

Dart null / false / empty checking: How to write this shorter?

深陷 2025-02-13 07:17:58

尝试以下操作:

try {
 doSomething();
} on RangeError catch (_) {
 handleException();
}

Try this:

try {
 doSomething();
} on RangeError catch (_) {
 handleException();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文