List <附件>无效值:仅有效值为0:1

如何解决List <附件>无效值:仅有效值为0:1

我有3个按钮,当用户单击该按钮时,他们会拍照并在照片预览中更改该按钮。长按则删除该图片,并用第一个按钮替换。尝试删除照片时,出现以下错误:

Unhandled Exception: RangeError (index): Invalid value: Only valid value is 0: 1
E/flutter ( 4122): #0      List.[] (dart:core-patch/growable_array.dart:146:60)
E/flutter ( 4122): #1      _HomePageNoticeProblemState.removePicture.<anonymous closure> (package:flutterapperadauti/notice_a_problem/main_page.dart:117:23)
E/flutter ( 4122): #2      State.setState (package:flutter/src/widgets/framework.dart:1233:30)
E/flutter ( 4122): #3      _HomePageNoticeProblemState.removePicture (package:flutterapperadauti/notice_a_problem/main_page.dart:113:5)
E/flutter ( 4122): #4      _HomePageNoticeProblemState.build.<anonymous closure> (package:flutterapperadauti/notice_a_problem/main_page.dart:509:23)
E/flutter ( 4122): #5      _InkResponseState._handleLongPress (package:flutter/src/material/ink_well.dart:805:25)
E/flutter ( 4122): #6      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:865:61)
E/flutter ( 4122): #7      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter ( 4122): #8      LongPressGestureRecognizer._checkLongPressStart (package:flutter/src/gestures/long_press.dart:304:7)
E/flutter ( 4122): #9      LongPressGestureRecognizer.didExceedDeadline (package:flutter/src/gestures/long_press.dart:253:5)
E/flutter ( 4122): #10     PrimaryPointerGestureRecognizer.didExceedDeadlineWithEvent (package:flutter/src/gestures/recognizer.dart:501:5)
E/flutter ( 4122): #11     PrimaryPointerGestureRecognizer.addAllowedPointer.<anonymous closure> (package:flutter/src/gestures/recognizer.dart:454:40)
E/flutter ( 4122): #12     _rootRun (dart:async/zone.dart:1180:38)
E/flutter ( 4122): #13     _CustomZone.run (dart:async/zone.dart:1077:19)
E/flutter ( 4122): #14     _CustomZone.runGuarded (dart:async/zone.dart:979:7)
E/flutter ( 4122): #15     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1019:23)
E/flutter ( 4122): #16     _rootRun (dart:async/zone.dart:1184:13)
E/flutter ( 4122): #17     _CustomZone.run (dart:async/zone.dart:1077:19)
E/flutter ( 4122): #18     _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:1003:23)
E/flutter ( 4122): #19     Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:23:15)
E/flutter ( 4122): #20     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:398:19)
E/flutter ( 4122): #21     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:429:5)
E/flutter ( 4122): #22     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)
E/flutter ( 4122): 

在这一行:if(attachments[1] != null) {

我的列表属于附件类型

我删除图片的代码:

void removePicture(int i) async {
setState(() {
  if(i==1) {
    attachments.removeAt(0);
    recordedImage1 = null;
    if(attachments[1] != null) {
      attachments[0] = attachments[1];
      recordedImage1 = recordedImage2;
    }
    if(attachments[2] != null) {
      attachments[1] = attachments[2];
      recordedImage2 = recordedImage3;
      recordedImage3 = null;
    }
  }
  if(i==2) {
    recordedImage2 = null;
    attachments.removeAt(1);
    if(attachments[2] != null) {
      attachments[1] = attachments[2];
      recordedImage2 = recordedImage3;
      recordedImage3 = null;
    }
  }
  if(i==3) {
    attachments.removeAt(2);
    recordedImage3 = null;
  }
});

}

我观察到该图片实际上被删除了(如果我单击另一个按钮制作照片,它将被新图片替换),但是如果用户不拍摄其他照片,则该按钮不会更新。

如果我长按第一张图像会出现错误,但是如果我按下最后一个按钮并制作新图片,则新图片将替换第一张图片(因为它已被删除)

enter image description here

如果您需要更多详细信息,请询问

解决方法

我认为这更多是评论,但基于我的声誉,我不允许这样做。我想这是怎么回事:

您有一条指令:attachments.removeAt(0);,它将在检查attachments[1]中的值之前删除列表中的第一个元素。

我认为您应该检查数组的长度,或者在删除值时使用attachements[i] = null

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-