RangeError (index): Invalid value: Not in inclusive range 0..2: 3 i cant fix

Advertisements

i have a problem this my list

             class _FitnessAppState extends State<FitnessApp> {

String img_Header =
"https://images.unsplash.com/photo-1517836357463-d25dfeac3438?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80&quot;;
List trainingImage = [
"https://images.unsplash.com/photo-1534258936925-c58bed479fcb?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1631&q=80&quot;,
"https://images.unsplash.com/photo-1575052814086-f385e2e2ad1b?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80&quot;,
"https://media.istockphoto.com/photos/picture-of-people-running-on-treadmill-in-gym-picture-id879180126?k=20&m=879180126&s=612×612&w=0&h=WZ1Iqcqv5_rNTNslUscoMg9qAUoNiDG8kWBfVnpPapQ=&quot;,
];

>Solution :

your variables are:

String img_Header = "https://images.unsplash.com/photo-1517836357463-d25dfeac3438?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80";

List trainingImage = [
"https://images.unsplash.com/photo-1534258936925-c58bed479fcb?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1631&q=80",
"https://images.unsplash.com/photo-1575052814086-f385e2e2ad1b?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80",
"https://media.istockphoto.com/photos/picture-of-people-running-on-treadmill-in-gym-picture-id879180126?k=20&m=879180126&s=612x612&w=0&h=WZ1Iqcqv5_rNTNslUscoMg9qAUoNiDG8kWBfVnpPapQ=",
];

the error of RangeError is happened in trainingImage variable as it is a List datatype, that’s because you are trying to access an unavailable index inside a List, as example, your list trainingImage has only 3 elements which start by index 0 and ends by index 2:

print(trainingImage[0]);
// output: "https://images.unsplash.com/photo-1534258936925-c58bed479fcb?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1631&q=80"

print(trainingImage[1]);
// output: "https://images.unsplash.com/photo-1575052814086-f385e2e2ad1b?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80"

print(trainingImage[2]);
// output: "https://media.istockphoto.com/photos/picture-of-people-running-on-treadmill-in-gym-picture-id879180126?k=20&m=879180126&s=612x612&w=0&h=WZ1Iqcqv5_rNTNslUscoMg9qAUoNiDG8kWBfVnpPapQ="

if you are trying to access the index 3 you will get the error of RangeError:

print(trainingImage[3]);
// output: RangeError (index): Invalid value: Not in inclusive range 0..2: 3

the solution is to always check if an index is available before accessing it:

// as example you want to access the index 3
int index = 3;

// 1st way
if(trainingImage.asMap().containsKey(index))
{
// run this if index 3 is available
}

// 2nd way
if(index < trainingImage.length && index >= 0)
{
// run this if index 3 is available
}

Leave a ReplyCancel reply