Here is my are my List Items:
static List<Category> getMockedCategories(){
return[
Category(
name: "Ascent",
imgName: "AscentLoading",
subCategories: [
SubCategory(
name: "ascent",
imgName: "ascent_a_sova_frontgen"
)
]
),
Category(
name: "Breeze",
imgName: "BreezeLoading",
subCategories: [
]
),
The category class:
class Category{
String name;
String imgName;
List<Category>? subCategories;
Category(
{
required this.name,
required this.imgName,
this.subCategories
}
);
}
The Subcategory Class:
class SubCategory extends Category {
SubCategory({
required String name,
required String imgName,
}): super(
name: name,
imgName: imgName
);
}
When trying to create a ListView of my images with only a certain name in their imgName I am running into this error: ======== Exception caught by widgets library ======================================================= The following RangeError was thrown building SelectedCategoryPage(dirty): RangeError (index): Invalid value: Only valid value is 0: 1
If comment out the catList section and just have pointer pointing at the selectedCategory.subCategories it works fine. I can't see why its not working for catList. Any help is appreciated.
Here is the code for the page running into the issue:
var catList = [];
for(int j = 0; j <= selectedCategory.subCategories!.length; j ){
if( selectedCategory.subCategories![j].imgName.contains(globals.agentSelected) ){
catList.add(selectedCategory.subCategories![j]);
}
}
var pointer = selectedCategory.subCategories;
if(catList[0] != null){
pointer = catList.cast<Category>();
}
Tried creating another List of Categories to pass to listview builder but am receving an error on it: ======== Exception caught by widgets library ======================================================= The following RangeError was thrown building SelectedCategoryPage(dirty): RangeError (index): Invalid value: Only valid value is 0: 1
CodePudding user response:
Could you try changing:
j <= selectedCategory.subCategories!.length
to j < selectedCategory.subCategories!.length
j
cannot have a position equals to the length of subCategories.
You can also do this:
var pointer = selectedCategory.subCategories;
if(catList.isNotEmpty){
pointer = catList.cast<Category>(); //Try using a for loop here if this statement gives you an error
}