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.
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,
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"),
);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With