【问题标题】:setState() called after dispose(): _StatefulBuilderState#09642(lifecycle state: defunct, not mounted)setState() 在 dispose() 之后调用:_StatefulBuilderState#09642(生命周期状态:已失效,未安装)
【发布时间】:2020-12-28 02:29:46
【问题描述】:

我用底页显示内容,我在计算后更新文件夹大小,使用 statefulbuilder 的 setState,我也使用了挂载条件,然后在调用 dispose 后它也显示 setState,帮我解决这个问题。

FileStat fileStat = selectedFilesAndFolders.last.statSync();
  String size = '0 KB';
  showModalBottomSheet(
      context: context,
      shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.only(
        topLeft: Radius.circular(12.0),
        topRight: Radius.circular(12.0),
      )),
      builder: (context) {
        return StatefulBuilder(builder: (context, newSetState) {
          getFolderSize(selectedFilesAndFolders.last.path).then((value) {
            if (!mounted) return;

            newSetState(() {
              size = value;
            });
          });

          return SingleChildScrollView(
            child: Column(
              children: [
                SizedBox(
                  height: 2.0,
                ),
                Text(
                  'Info',
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                  ),
                ),
                SizedBox(
                  height: 2.0,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Expanded(
                        child: Align(
                            alignment: Alignment.centerRight,
                            child: Text('Name: '))),
                    Expanded(
                        child: Text(path
                            .basename(selectedFilesAndFolders.last.path)))
                  ],
                ),
                SizedBox(
                  height: 2.0,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Expanded(
                        child: Align(
                            alignment: Alignment.centerRight,
                            child: Text('Size: '))),
                    Expanded(child: Text(size)),
                  ],
                ),
                SizedBox(
                  height: 2.0,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Expanded(
                        child: Align(
                            alignment: Alignment.centerRight,
                            child: Text('Last Modified Date: '))),
                    Expanded(child: Text(fileStat.modified.toString()))
                  ],
                ),
                SizedBox(
                  height: 2.0,
                ),
              ],
            ),
          );
        });
      });

【问题讨论】:

    标签: android flutter dart async-await flutter-showmodalbottomsheet


    【解决方案1】:

    试试下面的

    if (!mounted){
        setState(() {
            size = value;
        }),
    };
    

    【讨论】:

    • 如果我使用上述条件,那么当底部表打开时 setState 将不起作用。我试过你的代码。现在没有出现错误,但是没有调用 setState 来更新大小。
    • 在您的代码中,您调用了newSetState,但您似乎需要调用setState,我已经更新了我的答案以反映这一点
    猜你喜欢
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 2021-06-26
    • 2018-08-26
    • 1970-01-01
    • 2021-01-10
    • 2021-04-16
    • 2019-10-28
    相关资源
    最近更新 更多