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

时间:2020-08-14 13:47:56

标签: image flutter attachment

我有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

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

1 个答案:

答案 0 :(得分:1)

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

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

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