gcsar
gcsar

Reputation: 69

Rebuild cause "Stream has already been listened to"

I have BehaviorSubject that emits a list of items from a Firebase query everytime I type in a TextField, like a full-text search. I use a StreamBuilder (inside a ListView) to listen to this stream and display all list's items in a Column wrapped with an AnimatedSwitcher.

When I try to scroll the view or when the column refreshes I get "Stream has already been listened to". I tried everything but I was not able to fix it.

Edit: I added some code

view.dart

PostController _postController = new PostController();

ListView(
    padding: EdgeInsets.all(12),
    children: <Widget>[

      //titolo
      Container(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(10)),
          color: Color.fromRGBO(242, 242, 242, 1),
        ),
        padding: EdgeInsets.all(8),
        child: Column( 
          children: <Widget>[
            StreamBuilder(
              stream: _postController.titleStream$,
              builder: (BuildContext context, AsyncSnapshot<String> snap) {
                return TextField(
                  onChanged: (String search) => _postController.updateTitle(search),
                  decoration: InputDecoration(
                    contentPadding: EdgeInsets.all(12),
                    border: InputBorder.none,
                    fillColor: Colors.transparent,
                    filled: true,
                    hintText: "titolo",
                    errorText: snap.error
                  ),
                  style: TextStyle(color: Colors.black, fontWeight: FontWeight.bold, fontSize: 24),
                );
              },
            ),

            StreamBuilder(
              stream: _postController.superStream,
              builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snap){
                return AnimatedSwitcher(
                  duration: Duration(milliseconds: 500),
                  transitionBuilder: (child, animation) => SizeTransition(sizeFactor: animation, child: child, axisAlignment: -1,),
                  child: snap.hasData ? BuildQuestions(snap.data.documents) : Container(width: 0, height: 0)
                );
              },
            ),

postController.dart

BehaviorSubject _titleController = new BehaviorSubject<String>();
Observable<String> get titleStream$ => 
_titleController.stream.transform(_validateTitle); //if string is not empty it emits otherwise it emits an error
String get title => _titleController.value;
void updateTitle(String title) => _titleController.add(title);

Observable<QuerySnapshot> get superStream => Observable.combineLatest2(
  titleStream$, 
  Queries.search(_titleController.value), //search stream from firestore api
  (String title, QuerySnapshot snap) => snap
);

SOLVED

I had a submit button inside a StreamBuilder that was listening to another Observable which I use to check if the form's field are not empty. When I type in the TextField and the Column is displayed, the Submit button slide down out of the view, when I scroll down towards it the StreamBuilder is rebuilded and tries to listen to his stream again and this generates the error. Solved with:

Observable<bool> get validate => Observable.combineLatest2(titleStream$, descriptionStream$, (t, d) => true).asBroadcastStream();

Upvotes: 0

Views: 335

Answers (2)

Bibek Saha
Bibek Saha

Reputation: 213

    useCase.streamData().listen((event) {
      _internalStream.sink.add(event);
    });


  final _internalStream = BehaviorSubject<Type>();
  ValueStream<Type> get listenableStream => _internalStream.stream;

Attached sample code for reference

Upvotes: 0

Murat Aslan
Murat Aslan

Reputation: 1580

declare your stream as broadcast stream as below

i use rxdart you should modify it with your scenario

  static var controller =StreamController<Map<String, dynamic>>();
  static var _streamObservable = Observable(controller.stream.asBroadcastStream());

Upvotes: 0

Related Questions