flutter 重建原因“流已被监听”

ergxz8rk  于 2023-05-23  发布在  Flutter
关注(0)|答案(2)|浏览(137)

我有BehaviorSubject,每当我键入TextField时,它都会从Firebase查询中发出一个项目列表,就像全文搜索一样。我使用StreamBuilder(在ListView中)来监听这个流,并在用AnimatedSwitcher Package 的Column中显示所有列表的项目。
当我尝试滚动视图或刷新列时,我得到“Stream has already been listened to”。我试了所有的方法,但我无法修复它。

编辑:我添加了一些代码

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

t3psigkw1#

将您的流声明为广播流,如下所示
我使用rxdart你应该修改它与您的方案

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

vnzz0bqm2#

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

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

随附样品代码供参考

相关问题