【问题标题】:Flutter - setState() does not update list in widgetFlutter - setState() 不更新小部件中的列表
【发布时间】:2021-03-17 20:46:01
【问题描述】:

作为培训的一部分,我目前正在进行一个项目。我需要创建一个调度 Flutter 应用程序。但是,我在更新要显示的列表时遇到问题。

我有一个名为 handleRefresh 的方法:在初始化时加载列表(在 initState() 中调用),当我使用 refreshIndicator 刷新页面时。

列表在启动时加载良好,但随后修改时,刷新方法不起作用。

为了刷新显示,我清除列表,然后添加新元素。但是,它不起作用。 如果我决定不清除列表而只添加元素,它可以工作,但我有重复项,因为我在添加新元素之前没有清除列表。

那么,你知道为什么清除列表会阻止它被刷新吗?

对不起,我的英语不太好,我是法国人

class _PageControlesState extends State<PageControles>
    with AutomaticKeepAliveClientMixin<PageControles> {
  List<SemaineCc> _listeSemaineCc = new List<SemaineCc>();

  Temp body;

  List<Widget> _semaineCcWrapper;

  final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey =
      new GlobalKey<RefreshIndicatorState>();


  @override
  void initState() {
    super.initState();

    _semaineCcWrapper = new List<Widget>();
    _semaineCcWrapper.add(new Align(
      alignment: Alignment.center,
      child: CircularProgressIndicator(),
    ));
    _handleRefresh(debut: true);
  }

  @override
  // ignore: must_call_super
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        elevation: 0,
        title: Container(
          margin: EdgeInsets.only(top: 50, bottom: 30),
          width: double.infinity,
          child: Text(
              'Contrôles',
              textAlign: TextAlign.center,
              style: TextStyle(
                fontSize: 30,
                fontWeight: FontWeight.w600,
                color: ThemeProvider.themeOf(context).data.textTheme.headline1.color,
              )
          ),
        ),
      ),
      body: RefreshIndicator(
        key: _refreshIndicatorKey,
        onRefresh: _handleRefresh,
        child: Container(
            width: MediaQuery.of(context).size.width,
            child: SingleChildScrollView(
              physics: const AlwaysScrollableScrollPhysics(),

              child: ConstrainedBox(
                constraints: BoxConstraints(
                  minHeight: MediaQuery.of(context).size.height-30-50-30-27,
                ),
                child: Align(
                  alignment: Alignment.topCenter,
                  child: Container(
                    margin: EdgeInsets.all(5),
                    width: double.infinity,
                    child:  Container(
                      margin: EdgeInsets.only(
                        bottom: 60
                      ),
                      padding: EdgeInsets.symmetric(vertical: 0, horizontal: 0),
                      width: double.infinity,
                      child: Column(
                        children: _semaineCcWrapper,
                      )
                    ),
                  ),
                ),
              ),
            )
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          showDialog(
            context: context,
            builder: (BuildContext context) => PopupAjouterControleUI(),
          ).then((value) {
            print(value);
            if(value!=null)
              ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('sauvegarde reussie')));
             _handleRefresh();
          });
        },
        child: const Icon(Icons.add),
        backgroundColor: Colors.blueAccent,
      ),
    );
  }




  Future<void> _handleRefresh({bool debut = false}) {
      return fetchCc().then((value) {
        setState(() {
          _semaineCcWrapper.clear();

          _listeSemaineCc = value;
          for (SemaineCc semaineCc in value) {
            _semaineCcWrapper.add(SemaineCcUI(semaineCc: semaineCc));
          }
        });
      });
  }

  @override
  bool get wantKeepAlive => true;
}

【问题讨论】:

  • 欢迎来到 Stack Overflow!你能编辑你的帖子并添加一个来自fetchCc()的数据的例子吗?刷新操作之前和之后。另外,为什么你需要GlobalKey?你似乎没有使用它。
  • 谢谢你的回答,我昨天解决了这个问题。我会发布我的补丁。再次感谢!!!

标签: flutter refresh setstate


【解决方案1】:

我昨天设法解决了我的问题,

不知道为什么,但是在显示 stateFulWidget 列表时,显示有一种持久性。

例如:当我按以下顺序列出 3 个项目(3 个 stateFulWidget)时:A、B、C 如果我想删除 B 并在之后刷新,我们将看到 A、B 而不是 A、C。我不知道为什么,但就是这样。

所以为了解决这个问题,我没有删除B,而是将C的所有信息放在B中,使B成为C,然后删除之前的C。它对我有用。

希望能帮到你^^。

对不起,我的英语不太好,我是法国人

【讨论】:

    猜你喜欢
    • 2020-08-18
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 2018-07-28
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    • 2021-05-01
    相关资源
    最近更新 更多