flutter - 如何完全处理我的 Stateful Widget?

标签 flutter dispose setstate stateful

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

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()

我的问题是当我使用抽屉后退按钮时,我怎样才能完全处置我的 Stateful Widget?

这是小部件的调用

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

这就是 BillsWaterPay 小部件

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 - 如何完全处理我的 Stateful Widget?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52288613/

相关文章:

flutter - 如何在 flutter 中创建具有固定宽度和高度的颜色框?

flutter - flutter ,为什么在调用构建方法时不渲染ListView <Widget>?

.net - 在 Windows 窗体中添加和删除控件和内存使用

c# - .NET PictureBox - 如何确保资源已释放

c# - 在表单中处理自定义内存中控件

flutter - 调用setState时如何强制flutter重建特定的小部件

flutter - 将 Provider 与 TickerProviderStateMixin 一起使用 : Where can I initialise my controller?

list - 如何从数组中删除现有项目

javascript - 使用计算对象属性名称进行嵌套对象解构 - react 状态

android - 使用 Google Maps Flutter 进行 Android 预启动测试的 StackOverflowError