重建原因“流已被收听”

时间:2019-09-11 13:08:01

标签: flutter dart rxdart

我有BehaviorSubject,每次我输入TextField时都会从Firebase查询中发出项目列表,例如全文搜索。 我使用StreamBuilder(在ListView中)侦听此流,并在用AnimatedSwitcher包裹的Column中显示所有列表项。

当我尝试滚动视图或刷新列时,我得到“流已被收听”。我尝试了所有内容,但无法修复。

修改:我添加了一些代码

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
);

已解决

我在StreamBuilder中有一个提交按钮,该按钮正在侦听另一个Observable,可用来检查表单字段是否为空。当我键入TextField并显示Column时,Submit按钮从视图中滑出,当我向下滚动时,将重新构建StreamBuilder并尝试再次收听其流,这将产生错误。 解决:

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

1 个答案:

答案 0 :(得分:0)

将您的流声明为广播流,如下所示:

我使用rxdart,您应该根据自己的情况对其进行修改

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