Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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;,
];

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading