如何完全处置我的有状态小组件?

Nic*_*ick 4 dispose stateful setstate flutter

我调用有状态的小部件页面,并从服务器获取一些信息。如果未找到任何信息,则会警告用户没有任何信息。从抽屉后退按钮,我返回上一页。如果我非常快地来回重复,则会在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() 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().
E/flutter (22681): #0      State.setState.<anonymous closure> 
Run Code Online (Sandbox Code Playgroud)

在我的全状态小部件中@override void initState(),我也有@override void dispose()

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

这是小部件的要求

onTap: () {
Navigator.push(
  context,
  SlideRightRoute(widget: new BillsWaterPay(“S02")),
);
}
Run Code Online (Sandbox Code Playgroud)

这是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;
}
Run Code Online (Sandbox Code Playgroud)

Gün*_*uer 12

我认为问题是由处理小部件后服务器到达的响应引起的。

在调用之前,请检查窗口小部件是否已安装setState。这应该可以防止您看到的错误:

if no data
  if(mounted) {
    setState
        _noDataText = “No Data”;
  }
Run Code Online (Sandbox Code Playgroud)


小智 9

setState()由于异步,我们在处理小部件后调用时会显示上述异常。

通过使用 flutter => 的属性来处理这个 mounted

mounted 讲述小部件的可用性。

...
if (mounted) {
    setState(() {
        ...
    });
}
...
Run Code Online (Sandbox Code Playgroud)