我调用我的有状态小部件页面并从服务器获取一些信息.如果没有找到任何信息,它会警告用户没有任何信息. 从抽屉的后退按钮,我返回到上一页.如果我保持非常快地来回重复,我在IntelliJ IDE AS中的控制台消息上得到一个错误;

E/flutter (22681): [ERROR:flutter/shell/common/shell.cc(181)] Dart Error: Unhandled exception:
E/flutter (22681): setState() called after dispose(): _BillsPayWaterState#66be5(lifecycle state: defunct, not mounted)
E/flutter (22681): This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error can occur when code calls setState() from a timer or an animation callback. The preferred solution is to cancel the timer or stop listening to the animation in the dispose of the () callback. Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree.
E/flutter (22681): This error might indicate a memory leak if setState() is being called because another object is retaining a reference to this State object after it has been removed from the tree. To avoid memory leaks, consider breaking the reference to this object during dispose of ().
E/flutter (22681): #0      State.setState.<anonymous closure> 

在我的statefull小部件中,我有@override void initState()个,也有@override void dispose()

我的问题是,当我使用抽屉后退按钮时,如何完全处理我的有状态小部件?

This is the call for widget

onTap: () {
Navigator.push(
  context,
  SlideRightRoute(widget: new BillsWaterPay(“S02")),
);
}

And this the BillsWaterPay widget

List<List<dynamic>> _userAskBills;

// TODO: KURUM KODU - SABIT TELEFON
String _kurumKodu = "";
String _noDataText = "";

class BillsWaterPay extends StatefulWidget {
  final String title;

  BillsWaterPay(this.title);

  @override
  _BillsWaterPayState createState() =>
      _BillsWaterPayState();
}

class _BillsWaterPayState extends State<BillsWaterPay> {

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _kurumKodu = widget.title;
    _userAskBills;
    _noDataText;
    _loadEverything();
}

Future _loadEverything() async {
    await _getDataFromServer();
}

Future _getDataFromServer() async() {
    …
    …
    if no data
        setState
            _noDataText = “No Data”;  // In here I get an error setState called after dispose

}


@override
void dispose() {
  super.dispose();
      _kurumKodu = widget.title;
    _userAskBills;
    _noDataText;
}

推荐答案

我假设问题是由小部件被释放后来自服务器的响应到达引起的.

在您呼叫setState之前,请判断小工具是否已挂载.这应该可以防止您看到的错误:

// Before calling setState check if the state is mounted. 
if (mounted) { 
  setState (() => _noDataText = 'No Data');
}

Flutter相关问答推荐

从Flutter应用程序解释蓝牙BLE数据:了解Sylvania压力值

手势捕获的异常:类型';Double';不是类型转换中类型';Double';的子类型

如何使用新的Riverpod语法将依赖传递给AsyncNotifier?

从底部开始固定,然后使其可拖曳Flutter

CLI数据库连接后将SHA-1密钥添加到Firebase项目

flutter -当使用SingleChildScrollView包装我的列小部件时,它不会填充整个高度

如何使用Dio/Ffltter(前端)和amp;Go(后端)向API发送正确的请求

为什么在setState之后Ffltter Pageview重置为第0页

在背景图像上排列定位徽标

Wired Firebase错误-[CLOUD_FIRESTORE/UNAvailable]该服务当前不可用

在flutter中实现类似3D的可滚动堆叠分页列表

Flutter类调用另一个类中的另一个方法失败

Flutter屏幕适配 - 在模拟器和真实手机上文本尺寸不同

Firestore 使用 Flutter 以奇怪的格式保存数据

Flutter: pie_chart 插件动画滚动问题

如何修复 ICU Lexing 错误:Flutter 中的意外字符

在不丢失游戏状态的情况下从 Flame 游戏导航到 Flutter 小部件

如何根据字符串值动态构建和显示小部件?

无法将参数类型Future Function(String?)分配给参数类型void Function(NotificationResponse)?

如何将所有行项目平等地放在一行中?Flutter