farhanfadila1717 / slide_countdown

A Flutter package to create easy slide animation countdown / countup timer.
https://pub.dev/packages/slide_countdown
MIT License
60 stars 42 forks source link

"Bad state: Stream has already been listened to" in 1.0.2 #51

Closed SkyOfFeather closed 11 months ago

SkyOfFeather commented 11 months ago

When I use hot reload, this error will be reported on 1.0.2. Strangely, I saw a more detailed description of 1.0.2 that fixed this error.

The following StateError was thrown building RawSlideCountdown: Bad state: Stream has already been listened to.

Milimeter commented 11 months ago

Same here. Having the same error ═══════ Exception caught by widgets library ═══════════════════════════════════ Bad state: Stream has already been listened to. The relevant error-causing widget was SlideCountdownSeparated payment_board.dart:91 ════════════════════════════════════════════════════════════════════════════════

farhanfadila1717 commented 11 months ago

Hi thanks for filling this issue. Just release dev version for fix this issue checkout slide_countdown: ^1.1.0-dev.1.

JgomesAT commented 11 months ago

The error is still present in 1.0.2

farhanfadila1717 commented 11 months ago

The error is still present in 1.0.2

@JgomesAT Please checkout pre release version https://pub.dev/packages/slide_countdown/versions/1.1.0-dev.2

tomquas commented 11 months ago

the following changes to duration_stream fixes the bad state issue; however, i am still a little confused about the stream handling in general and why SliceCountdown.didUpdateWidget disposes streams frequently...

@@ -12,7 +12,7 @@ class StreamDuration {
   }

   final StreamController<Duration> _streamController =
-      StreamController<Duration>();
+      StreamController<Duration>.broadcast();

   Duration _durationLeft = Duration.zero;

@@ -52,17 +52,18 @@ class StreamDuration {
   }

   void play() {
-    _streamSubscription = Stream<Duration>.periodic(
+    _streamSubscription ??= Stream<Duration>.periodic(
       config.periodic,
       (_) {
-        if (!(_streamSubscription?.isPaused ?? true)) {
-          if (_isCountUp) {
-            return _durationLeft += config.periodic;
-          } else {
-            return _durationLeft -= config.periodic;
-          }
+        final paused = _streamSubscription?.isPaused ?? true;
+        if (paused) return _durationLeft; // was: Duration.zero; why?
+
+        if (_isCountUp) {
+          _durationLeft += config.periodic;
+        } else {
+          _durationLeft -= config.periodic;
         }
-        return Duration.zero;
+        return _durationLeft;
       },
     ).listen(
tomquas commented 11 months ago

followup: i activated ExampleControlDuration() in example.dart, and found the play, resume behavior to be broken.

removing SlideCountdown#didUpdateWidget entirely fixed this.

farhanfadila1717 commented 11 months ago

@tomquas i have release a new version, remove didUpdateWidget from both SlideCountdown and SlideCountdownSeparated.

tomquas commented 11 months ago

@farhanfadila1717 thank you! may i ask: was didUpdateWidget an evil leftover from past, or what was the intention behind it?

farhanfadila1717 commented 11 months ago

When I refactor SlideCountdown to improve performance, full customize i also upgrade StreamDuration package. The error seems come from StreamDuration reinit when didUpdateWidget called.

synstin commented 11 months ago

@farhanfadila1717 1.2.1 After the upgrade, the error does not occur, but the textStyle property is not applied.

farhanfadila1717 commented 11 months ago

@synstin yep the property depercated, try style instead

synstin commented 11 months ago

@farhanfadila1717 It works perfectly. Thank you.