'Contribution in solving the error: RangeError (index): Invalid value: Valid value range is empty: 0

RangeError (index): Invalid value: Valid value range is empty: 0 I am coding an app in which this error (from the list: listeCmdes) was issuing sometimes. The list gets it’s values from a provider. When it is empty, I display a message: ’Nothing found in the database’. If an element is founded in the list, it is displayed in a Container() widget. In that widget I have 2 other icons: one for deleting the element, another for editing it. The deleting icon works fine. But the edit icon, when cliked, displays the famous error: RangeError (index): Invalid value: Valid value range is empty: 0. I unsuccessfully searched the solution on the net. I tried many things (for example checking if the index of the wanted element is lower than the lenght of the list, if the list is not empty, etc.). Nothing helped. The last thing I did is to create a temporisation before accessing the list’s element : await Future.delayed(Duration(miliseconds: 1)); The error disapeared: no more trouble of this kind on that place! I tried even a value of 0 for the Duration and it was still ok. I can not explain it, because I am new in DART/FLUTTER, so just try the thing if you have the same trouble.

                          IconButton(
                            onPressed: () async {
                              await Future.delayed(
                                  Duration(milliseconds: 1));
                              await Navigator.of(context).push(
                                MaterialPageRoute(
                                  builder: (context) => TraitementCmde(
                                      cmde2Edit: listeCmdes[idx],
                                      bandeExploitee: bandeEnCours),
                                ),
                              );
                            },
                            icon: Icon(Icons.edit),
                            color: Colors.blue,
                          ),


Sources

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

Source: Stack Overflow

Solution Source