Reputation: 11
I'm making a flutter App for a project in my School and I have a problem. I have a page where there are widgets representing categories of articles and when a category is clicked a page with articles from that category is displayed. The problem is that once a category is called, the articles in that category remain the same despite the category change. When the page is called, a controller is created that will execute the query that retrieves the items in the category.
How can I get this controller to remind me every time the page is loaded?
Category page code :
class ProduceCategoryScreen extends StatefulWidget {
static String routeName = "/produceByCategorie";
@override
State<ProduceCategoryScreen> createState() => _ProduceCategoryScreenState();
}
class _ProduceCategoryScreenState extends State<ProduceCategoryScreen> {
static int gridColumn = 1;
static ArticleByCategoryController articleController =
Get.put(ArticleByCategoryController());
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Produitss"),
),
body: Column(
children: [
Padding(
padding: const EdgeInsets.all(16),
child: Row(
children: [
Expanded(
child: Text(
'Green Tomato',
style: TextStyle(
color: Colors.black,
fontSize: 32,
fontWeight: FontWeight.w900,
),
),
),
IconButton(
onPressed: () {
switch (gridColumn) {
case 1:
setState(() {
gridColumn = 2;
});
break;
case 2:
setState(() {
gridColumn = 1;
});
break;
default:
}
},
icon: Icon(Icons.grid_view),
)
],
),
),
Expanded(
child: Obx(
() {
if (articleController.isLoading.value)
return Center(child: CircularProgressIndicator());
else
return AlignedGridView.count(
crossAxisCount: gridColumn,
itemCount: articleController.articleList.length,
mainAxisSpacing: 16,
crossAxisSpacing: 16,
itemBuilder: (context, index) {
return ProductTile(articleController.articleList[index]);
},
);
},
),
),
],
),
);
}
}
If you want more information you can send me a message on my discord : PascheK7#6324.
Upvotes: 1
Views: 317
Reputation: 249
I think you are passing same list of data everytime when you execute below line.
"return ProductTile(articleController.articleList[index]);"
articleController.articleList : does this list contain only one category data ?
or it is contain category wise data.
e.g. : articleController.articleList[0] = category 1 list
articleController.articleList[1] = category 2 list
articleController.articleList[2] = category 3 list --> this way, it shouldn't make problem.
If articleList contain only one category data, then issue can happen that you get same data everytime. bcoz you are passing only index, but not category wise data.
Upvotes: 1