I can’t get my code to stop looping
class CategoriesScreenCopy extends ConsumerStatefulWidget {
const CategoriesScreenCopy({super.key});
@override
ConsumerState<CategoriesScreenCopy> createState() => _CategoriesScreenState();
}
class _CategoriesScreenState extends ConsumerState<CategoriesScreenCopy> {
List<Widget> drinkCategories = [];
Widget highPDrinks = ListView();
Widget highFDrinks = ListView();
Widget lowBSDrinks = ListView();
Widget getHighProteinDrinks() {
for (final drink in highProteinDrinks) {
highPDrinks = ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: highProteinDrinks.length,
itemBuilder: (ctx, index) => DrinkItem(
drink: highProteinDrinks[index],
),
);
}
drinkCategories.add(getHighFiberDrinks());
return highPDrinks;
}
Widget getHighFiberDrinks() {
for (final drink in highFiberDrinks) {
highFDrinks = ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: highFiberDrinks.length,
itemBuilder: (ctx, index) => DrinkItem(
drink: highFiberDrinks[index],
),
);
}
drinkCategories.add(highFDrinks);
return highFDrinks;
}
@override
void initState() {
super.initState();
getHighProteinDrinks();
getHighFiberDrinks();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: GridView.builder(
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 1,
childAspectRatio: 3 / 3,
crossAxisSpacing: 20,
mainAxisSpacing: 8,
),
//itemCount: drinkCategories.length,
itemBuilder: (context, index) => CategoryViewer(
categoryDrinkList: getHighFiberDrinks(),
categoryDrinkName: 'High-Protein Drinks',
),
),
);
}
}
I am just trying to render 2 instances of CategoryViewer because only 2 of them should be in drinkCategories
. The getHighProteinDrinks()
Widget Function adds 1 too the list & the getHighFiberDrinks()
is supposed to add the other, but for some reason I have an endless stream of the same CategoryViewer with the same child ListView widget. I know this is supposed to be simple but I am new to Flutter. Some help would be appreciated.