美文网首页
Flutter中setState导致的内存泄漏——setStat

Flutter中setState导致的内存泄漏——setStat

作者: tinyvampirepudg | 来源:发表于2019-07-12 09:52 被阅读0次

    Flutter中setState导致的内存泄漏——setState() called after dispose()

    错误原因

    flutter端请求网络时,调用的是宿主App的网络请求。

    flutter通过消息通道发送一个消息,然后await等待消息返回,最终宿主app会调用reply.reply(obj)方法返回数据。如果在这个过程中,flutter页面关闭,就会出现如下异常,类似Android中的内存泄漏。

    2019-04-29 14:01:43.593 10294-10635/com.tinytongtong.flutter E/flutter: [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: setState() called after dispose(): _SystemMessageListRouteState#017f6(lifecycle state: defunct, not mounted)
        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.
        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().
    

    解决方式

    上面的日志说的很明白。

    我们的错误原因是异步消息未返回,所以在setState方法之前调用mouted属性进行判断即可。具体示例如下:

          if(mounted){
            setState(() {
              _listData.addAll(list);
          }
    

    相关文章

      网友评论

          本文标题:Flutter中setState导致的内存泄漏——setStat

          本文链接:https://www.haomeiwen.com/subject/zanhkctx.html