未处理的异常:RangeError(索引):无效值:有效值范围为空:0-颤振

时间:2020-04-08 19:15:06

标签: function for-loop flutter

我有一套最简单的代码,在Flutter上似乎不起作用。这是一个简单的for循环,似乎给了例外。

List vendorMainCategory = ['one', 'two', 'three', 'four']
List testList = [];
List testList1 = [];

testFunction () {

testList1 = vendorMainCategory
print(testList1)

for(int i =0; i<4; i++){

testList[i] = testList1[i]

   }
}

在控制台中可以看到,该函数一直运行到'print(testList1)'行,并在循环开始时遇到错误。似乎无法弄清楚为什么会这样。任何帮助将不胜感激。

在使用FlatButton的onPressed属性调用此函数时,会在控制台中看到以下内容:

I/flutter ( 5687): [one, two, three, four]
E/flutter ( 5687): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] 
Unhandled Exception: RangeError (index): Invalid value: Valid value range is empty: 0
E/flutter ( 5687): #0      List._setIndexed (dart:core-patch/growable_array.dart:152:72)
E/flutter ( 5687): #1      List.[]= (dart:core-patch/growable_array.dart:149:5)
E/flutter ( 5687): #2      _TestPage1State.newFunction (package:fetchapp/pages/test_page1.dart:80:16)
E/flutter ( 5687): #3      _TestPage1State.build.<anonymous closure> (package:fetchapp/pages/test_page1.dart:109:29)
E/flutter ( 5687): #4      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:779:14)
E/flutter ( 5687): #5      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:862:36)
E/flutter ( 5687): #6      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter ( 5687): #7      TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:486:11)
E/flutter ( 5687): #8      BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:264:5)
E/flutter ( 5687): #9      BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:199:7)
E/flutter ( 5687): #10     PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:470:9)
E/flutter ( 5687): #11     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:76:12)
E/flutter ( 5687): #12     PointerRouter._dispatchEventToRoutes.<anonymous closure> (package:flutter/src/gestures/pointer_router.dart:117:9)
E/flutter ( 5687): #13     _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:379:8)
E/flutter ( 5687): #14     PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:115:18)
E/flutter ( 5687): #15     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:7)
E/flutter ( 5687): #16     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:218:19)
E/flutter ( 5687): #17     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
E/flutter ( 5687): #18     GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
E/flutter ( 5687): #19     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
E/flutter ( 5687): #20     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
E/flutter ( 5687): #21     _rootRunUnary (dart:async/zone.dart:1138:13)
E/flutter ( 5687): #22     _CustomZone.runUnary (dart:async/zone.dart:1031:19)
E/flutter ( 5687): #23     _CustomZone.runUnaryGuarded (dart:async/zone.dart:933:7)
E/flutter ( 5687): #24     _invoke1 (dart:ui/hooks.dart:275:10)
E/flutter ( 5687): #25     _dispatchPointerDataPacket (dart:ui/hooks.dart:184:5)
E/flutter ( 5687): W/IInputConnectionWrapper( 5687): getExtractedText on inactive InputConnection
W/IInputConnectionWrapper( 5687): getTextBeforeCursor on inactive InputConnection
D/Surface ( 5687): Surface::disconnect(this=0x7e08d6d000,api=1)
D/Surface ( 5687): Surface::disconnect(this=0x7e08d6d000,api=-1)
D/Surface ( 5687): Surface::disconnect(this=0x7e15bfa000,api=1)
V/PhoneWindow( 5687): DecorView setVisiblity: visibility = 4, Parent = android.view.ViewRootImpl@9a76da, this = DecorView@897370b[MainActivity]

1 个答案:

答案 0 :(得分:1)

如何尝试使用add

List vendorMainCategory = ['one', 'two', 'three', 'four'];
List testList = [];
List testList1 = [];

testFunction () {

testList1 = vendorMainCategory;
print(testList1);

for(int i =0; i<4; i++){

testList.add(testList1[i]); //<--changed to add

   }
}
  testFunction();
print(testList); //[one, two, three, four]