You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter/GetX 页面重载时如何重新发起分类文章数据请求

问题根因
  • 你给gridColumn、articleController两个变量加了static关键字,这俩变量属于类全局持有,第一次打开分类页初始化控制器、加载完数据后,后续切换分类打开页面时,不会重新创建控制器实例,会一直复用第一次加载的带旧数据的控制器。
  • 没有在页面初始化生命周期里主动触发数据拉取逻辑,控制器仅在第一次Get.put时执行过一次查询,后续页面重建不会重新调用查询方法。
  • 跳转分类页时没有将当前选中的分类标识传给控制器,控制器无法感知分类切换,自然不会拉取对应分类的新数据。
修复方案
  1. 移除两个变量的static修饰,让每个页面实例持有独立的状态和控制器实例,避免全局复用导致的数据串流。
  2. 在initState生命周期中接收路由传递的分类参数,主动调用控制器的数据拉取方法,保证每次打开页面都会触发新分类的数据请求。
  3. 跳转分类页时必须携带对应分类的ID、名称等参数,控制器的数据拉取方法要接收分类ID作为查询条件,请求前先清空旧的文章列表,避免旧数据残留。
修复后完整代码
class ProduceCategoryScreen extends StatefulWidget {
  static String routeName = "/produceByCategorie";

  @override
  State<ProduceCategoryScreen> createState() => _ProduceCategoryScreenState();
}

class _ProduceCategoryScreenState extends State<ProduceCategoryScreen> {
  // 移除static修饰
  int gridColumn = 1;
  late ArticleByCategoryController articleController;
  late Map categoryParams;

  @override
  void initState() {
    super.initState();
    // 初始化控制器
    articleController = Get.put(ArticleByCategoryController());
    // 获取路由传递的分类参数
    categoryParams = Get.arguments;
    // 页面初始化时触发对应分类的文章拉取
    articleController.fetchArticleList(categoryParams['categoryId']);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(categoryParams['categoryName'] ?? "分类文章"),
      ),
      body: Column(
        children: [
          Padding(
            padding: const EdgeInsets.all(16),
            child: Row(
              children: [
                Expanded(
                  child: Text(
                    categoryParams['categoryName'] ?? 'Green Tomato',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 32,
                      fontWeight: FontWeight.w900,
                    ),
                  ),
                ),
                IconButton(
                  onPressed: () {
                    setState(() {
                      gridColumn = gridColumn == 1 ? 2 : 1;
                    });
                  },
                  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,
                    padding: const EdgeInsets.symmetric(horizontal: 16),
                    itemBuilder: (context, index) {
                      return ProductTile(articleController.articleList[index]);
                    },
                  );
              },
            ),
          ),
        ],
      ),
    );
  }
}
跳转分类页的参考代码

点击分类组件跳转时,必须传入对应分类的参数,示例如下:

// 分类组件的点击事件中调用
Get.toNamed(
  ProduceCategoryScreen.routeName,
  arguments: {
    "categoryId": currentClickedCategory.id, // 替换为你实际的分类id字段
    "categoryName": currentClickedCategory.name, // 替换为你实际的分类名称字段
  }
);

注意:控制器中的fetchArticleList方法需要接收分类id入参,请求接口前先执行articleList.clear()清空旧数据,再赋值新的请求结果,避免旧数据残留。

内容的提问来源于stack exchange,提问作者PascheK7

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.30 04:39:20