Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Periodic Timer can not dispose

Tags:

flutter

dart

Code content is not important. Just one problem timer can not dispose when I want to leave this page. When I leave from this page, sendMessage("message"); function continue to run. Is there any option to dispose this timer?

Timer timer;

@override
void initState() {
super.initState();
timer = Timer.periodic(new Duration(seconds: 5), (timer) async {
          setState(() {
            unicode++;
            unicodeString = unicode.toString();
            if (unicodeString.length < 6) {
              int different = 6 - unicodeString.length;
              for (var i = 0; i < different; i++) {
                unicodeString = "0" + unicodeString;
              }
            }
            sendMessage("meesage");
              showSnackBarWithKey("Message Sended !");
          });
    });
}
 @override
 void dispose() {
 timer.cancel();
 super.dispose();
}

The error is below.

EXCEPTION CAUGHT BY WIDGETS LIBRARY The following assertion was thrown while finalizing the widget tree: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 4182 pos 12: '_debugLifecycleState != _ElementLifecycle.defunct': is not true. Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause. In either case, please report this assertion by filing a bug on GitHub:

I use dispose timer, but it can not dispose timer. I could not solve this problem. Help, please.

like image 561
SefaUn Avatar asked Dec 23 '22 16:12

SefaUn


1 Answers

i findout issue after run your code, the main problem is, dispose is called on a widget when it is completely removed from the parent tree.

so when you route new page,

  1. Using push navigation, a new screen is added on top of current screen. hence the tree (of old screen) is not completely destroyed hence dispose is not called.
  2. using pop. the screen is removed so is the tree. hence dispose is called.
  3. using push replacement. new screen replaces old screen deleting the widget tree. so dispose is called.

and for code, try this. (main part is pushReplacement i am using this for navigation)

Navigator.pushReplacement(
               context, MaterialPageRoute(builder: (context) => SplashScreen()));

final code is,

 class TimerButton extends StatefulWidget {
      @override
      _TimerButtonState createState() => _TimerButtonState();
    }
    
    class _TimerButtonState extends State<TimerButton> {
      Timer _timer;
      @override
      void initState() {
        super.initState();
    
        _timer = Timer.periodic(new Duration(seconds: 5), (timer)  async{
          setState(() {
           /* unicode++;
            unicodeString = unicode.toString();
            if (unicodeString.length < 6) {
              int different = 6 - unicodeString.length;
              for (var i = 0; i < different; i++) {
                unicodeString = "0" + unicodeString;
              }
            }*/
            sendMessage("meesage");
            showSnackBarWithKey("Message Sended !");
          });
        });
      }
      @override
      void dispose() {
        _timer.cancel();
        super.dispose();
      }
      @override
      Widget build(BuildContext context) {
        // TODO: implement build
       return RaisedButton(
         onPressed: (){
           Navigator.pushReplacement(
               context, MaterialPageRoute(builder: (context) => SplashScreen()));
         },
         child: Text("data"),
       );
      }
    }
like image 197
shirsh shukla Avatar answered Jan 21 '23 07:01

shirsh shukla