Flutter - How to check if an index exists in a List

Viewed 57

There's function to select the chapters based on index

selectInitialChapters(index:1){
// code to select the chapter details based on index
 // chapter is a map
 final keys = chapters.keys.toList();
 final key = keys[index]; // the passed index may not exist
 final lesson = chapters[key];
}

The information on the map will be updated based on the topic. Some subjects might not have any chapters with them or might not have more than one chapter (if the English chapter will be updated with the English chapter, if the GK chapter with the GK chapter). How can I find out if the passed index is present in the List?

1 Answers

Convert your map to list and get the length of that list or if can find the length of the map then length - 1 >= index then index exists.

Related