Chirag Rajgariah
Chirag Rajgariah

Reputation: 57

Unhandled Exception: RangeError (index): Invalid value: Valid value range is empty: 0 - Flutter

I have the simplest set of code which does not seem to be working on Flutter. It is a simple for loop, which seems to be giving the exception.

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]

   }
}

As can be seen in the console, the function runs until the 'print(testList1)' line, and encounters an error as soon as the loop begins. Can't seem to figure out why this might be. Any help will be highly appreciated.

On calling this function using a FlatButton's onPressed property, I am shown this in the console:

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]

Upvotes: 0

Views: 706

Answers (1)

LonelyWolf
LonelyWolf

Reputation: 4392

How about to try using 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]

Upvotes: 1

Related Questions