'(FLUTTER) --- RangeError (index): Invalid value: Valid value range is empty: 0

Row( mainAxisAlignment: MainAxisAlignment.center, children: [ Padding( padding: const EdgeInsets.all(8.0), child: Icon( password[0] == null ? unchangedPassword : changedPassword, color: Colors.white, size: 20, ),

Can someone please explain why I can't write an if statement that checks if the value of the index is equal to a null value?



Solution 1:[1]

Use password.isEmpty instead of password[0] == null

Solution 2:[2]

password[0] doesn't exist. It appears password is a List

you could do password[0] == null if it was a map, but it's not. It's a list. That value doesn't exist. It's not null. It doesn't exist. So it blows up.

Why doesn't it exist? Because the memory wasn't allocated. Flutter will allocated the memory automatically when you need it. If you do password.add(value), then password[0] will exist at that point. But since you're getting the range error you haven't used password.add

For lists, use password.isNotEmpty or password.length == 0;

Solution 3:[3]

I have been also facing this issue and following checking solved my issue

Text("${(subjects.isEmpty) ? "" : subjects.name}"),

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ronan
Solution 2 Ovidius Mazuru
Solution 3 Mimu Saha Tishan