Skip to content

Commit 6a2a344

Browse files
authored
Unhandled Exception(setState called after dispose)
the color loader was giving Unhandled exceptions when you use it until something loading because it will keep listening to changes and timer ticks ,, just added timer.cancel to stop listening and setstating
1 parent 89b8420 commit 6a2a344

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

lib/loaders/color_loader.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class _ColorLoaderState extends State<ColorLoader>
1616
with SingleTickerProviderStateMixin {
1717
final List<Color> colors;
1818
final Duration duration;
19+
Timer timer;
1920

2021
_ColorLoaderState(this.colors, this.duration);
2122

@@ -59,7 +60,7 @@ class _ColorLoaderState extends State<ColorLoader>
5960

6061
tweenIndex = 0;
6162

62-
Timer.periodic(duration, (Timer t) {
63+
timer = Timer.periodic(duration, (Timer t) {
6364
setState(() {
6465
tweenIndex = (tweenIndex + 1) % colors.length;
6566
});
@@ -83,6 +84,7 @@ class _ColorLoaderState extends State<ColorLoader>
8384
@override
8485
void dispose() {
8586
super.dispose();
87+
timer.cancel();
8688
controller.dispose();
8789
}
8890
}

0 commit comments

Comments
 (0)