Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dispose of my Stateful Widget completely?

I call my stateful widget page and get some info from the server. If no info found it warns the user that there isn't any info. From the drawer back button, I go back to the previous page. If I keep repeat back and forth very fast I get an error on console message in my 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>  

In my statefull widget I have @override void initState() and I also have @override void dispose()

My question is when I use the drawer back button, How can I dispose of my Stateful Widget completely?

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; } 
like image 651
Nick Avatar asked Sep 12 '18 06:09

Nick


People also ask

How do you dispose of a stateful widget Flutter?

Flutter – dispose() Method with ExampleDispose is a method triggered whenever the created object from the stateful widget is removed permanently from the widget tree. It is generally overridden and called only when the state object is destroyed.

Do I need to dispose TextEditingController?

Remember to dispose of the TextEditingController when it is no longer needed. This will ensure we discard any resources used by the object. This example creates a TextField with a TextEditingController whose change listener forces the entered text to be lower case and keeps the cursor at the end of the input.

Does state object live after Dispose ()?

Yes, it's a…


1 Answers

I assume the problem is caused by the response from the server arrives after the widget is disposed.

Check if the widget is mounted before you call setState. This should prevent the error you are seeing:

// Before calling setState check if the state is mounted.  if (mounted) {    setState (() => _noDataText = 'No Data'); } 
like image 90
Günter Zöchbauer Avatar answered Sep 17 '22 12:09

Günter Zöchbauer