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

Flutter CircularProgressIndicator无法随isLoading更新状态问题

问题根因

Flutter中StatefulWidget的本地状态变量发生变更后,必须显式调用setState()方法通知框架重新执行组件的build逻辑,才能将最新状态同步渲染到UI上。
你的代码中在异步请求的then回调内直接执行isLoading = false,没有触发状态更新通知,页面始终使用初始的isLoading = true状态渲染,因此CircularProgressIndicator不会自动隐藏。

修复方式

修改fetchData方法的回调逻辑,将isLoading的赋值操作放入setState回调中即可:

void fetchData() async {
  Provider.of<ProductgridVM>(context,listen: false).fetchProductGridList(
      productcode: productcode,
      branchname: branchname,
      metalname: metalname,
      purityname: purityname,
      seriesname: seriesname,
      producttypename: producttypename,
      collectionname: collectionname,
      categoriesname: categoriesname,
      makingProcessname: makingProcessname,
      page_no: page_no, 
      page_size: page_size
  ).then((value) {
    setState(() {
      isLoading = false;
    });
  });
}
优化建议
  • 补充异步请求的异常捕获逻辑,在请求报错时也通过setState将isLoading置为false,同时可搭配错误提示组件告知用户请求失败,避免加载指示器永久显示。
  • 既然项目中已经使用Provider做状态管理,更推荐将isLoading加载状态统一维护在ProductgridVM中,页面通过Consumer/Selector监听加载状态变化刷新UI,减少页面State中的冗余状态维护。

内容的提问来源于stack exchange,提问作者Ajith S B

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.27 06:36:21