sooxie / list_treeview

A treeview for Flutter. Based on the listview.
MIT License
64 stars 23 forks source link

Bug: check if mounted before calling setState() #5

Closed MagnusJohansson closed 3 years ago

MagnusJohansson commented 3 years ago

Please consider checking if mounted before calling setState() in tree_view.dart https://github.com/sooxie/list_treeview/blob/c10335ca5645272618a9d4506a1ced9d47778f96/lib/tree/tree_view.dart#L78

From:

 void updateView() {
    setState(() => {});
  }

to

 void updateView() {
    if (mounted) {
      setState(() => {});
    }
  }

I/flutter ( 2032): The TreeViewController sending notification was: I/flutter ( 2032): Instance of 'TreeViewController' I/flutter ( 2032): ════════════════════════════════════════════════════════════════════════════════════════════════════ I/flutter ( 2032): ══╡ EXCEPTION CAUGHT BY FOUNDATION LIBRARY ╞════════════════════════════════════════════════════════ I/flutter ( 2032): The following assertion was thrown while dispatching notifications for TreeViewController: I/flutter ( 2032): setState() called after dispose(): _ListTreeViewState#b42b1(lifecycle state: defunct, not mounted) I/flutter ( 2032): This error happens if you call setState() on a State object for a widget that no longer appears in I/flutter ( 2032): the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error I/flutter ( 2032): can occur when code calls setState() from a timer or an animation callback. I/flutter ( 2032): The preferred solution is to cancel the timer or stop listening to the animation in the dispose() I/flutter ( 2032): callback. Another solution is to check the "mounted" property of this object before calling I/flutter ( 2032): setState() to ensure the object is still in the tree. I/flutter ( 2032): This error might indicate a memory leak if setState() is being called because another object is I/flutter ( 2032): retaining a reference to this State object after it has been removed from the tree. To avoid memory I/flutter ( 2032): leaks, consider breaking the reference to this object during dispose().

sooxie commented 3 years ago

The bug has been fixed