Search code examples
flutterdartjust-audio

Flutter plugin just_audio example error then dispose


I've just copied the example of just_audio library and put in into my project, and then I'm doing a hot reload or pressing back, I'm getting an error

[VERBOSE-2:shell.cc(209)] Dart Error: Unhandled exception:
Bad state: You cannot close the subject while items are being added from addStream
#0      Subject.close (package:rxdart/src/subjects/subject.dart:152:7)
#1      AudioPlayer.dispose (package:just_audio/just_audio.dart:611:30)
<asynchronous suspension>
#2      _LessonPractiseScreenState.dispose (package:wbh/ui/pages/lesson/lesson_practise_screen.dart:90:13)
#3      StatefulElement.unmount (package:flutter/src/widgets/framework.dart:4773:12)
#4      _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1935:13)
#5      _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1933:7)
#6      ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4600:14)
#7      _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1931:13)
#8      _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1933:7)
#9      SingleChildRenderObjec<…>

The causing widget as I tested is:

Widget get _seekBar => StreamBuilder<Duration>(
        stream: _player.durationStream,
        builder: (context, snapshot) {
          final duration = snapshot.data ?? Duration.zero;
          return StreamBuilder<Duration>(
            stream: _player.positionStream,
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                var position = snapshot.data ?? Duration.zero;
                if (position > duration) {
                  position = duration;
                }
                return SeekBar(
                  duration: duration,
                  position: position,
                  onChanged: (newPosition) {
                    _player.seek(newPosition);
                  },
                );
              } else
                return Container();
            },
          );
        },
      );

But I can't fix it by myself and have got no info on the net, so will appreciate any help.


Solution

  • This was reported as a bug via GitHub and a fix was rolled out soon after (see this issue for the discussion). Newer releases (>= 0.4.4) should resolve this issue.