错误:错误状态:流已被收听

Posted

技术标签:

【中文标题】错误:错误状态:流已被收听【英文标题】:Error: Bad state: Stream has already been listened to 【发布时间】:2021-12-26 23:50:32 【问题描述】:

我是 bloc 和 RxDart 的新手。一直在尝试放置一个非常简单的应用程序,以便能够验证用户登录输入并对其进行处理。在我介绍 RxDart 的 combineLatest 方法之前,代码运行良好。如果您发现导致此错误的原因,请查看并告诉我。

login_form_bloc.dart

class LoginFormBloc with Validators 
  //Initialise a streamController that will contain String data
  final _email = StreamController<String>();
  final _pwd = StreamController<String>();

  // Add data to stream
  // This function will recieve a String object
  Function(String) get changeEmail => _email.sink.add;
  Function(String) get changePwd => _pwd.sink.add;

  // Reference the stream
  // This stream will return String data
  Stream<String> get email => _email.stream.transform(validateEmail);
  Stream<String> get pwd => _pwd.stream.transform(validatePwd);
  Stream<bool> get submitValid =>
      CombineLatestStream.combine2(email, pwd, (a, b) => true);

  dispose() 
    _email.close();
    _pwd.close();
  

login_screen.dart

class LoginScreen extends StatelessWidget 

  Widget build(BuildContext context) 
    final loginBloc = LoginBlocProvider.of(context);
    return Scaffold(
      body: Container(
        margin: const EdgeInsets.all(20.0),
        child: buildForm(loginBloc),
      ),
    );
  

  Widget buildForm(LoginFormBloc loginBloc) 
    return Column(
      children: [
        emailField(loginBloc),
        passwordField(loginBloc),
        SizedBox(
          height: 20.0,
        ),
        buttonField('Submit', loginBloc),
      ],
    );
  

  Widget emailField(loginBloc) 
     //Some code
  

  Widget passwordField(loginBloc) 
    //Some code
  

  Widget buttonField(String label,loginBloc) 
    return StreamBuilder(
      stream: loginBloc.submitValid,
      builder: (context, dynamic snapshot) 
        return ElevatedButton(
          style:
              ButtonStyle(backgroundColor: MaterialStateProperty.all(Colors.pink)),
          child: Text(label),
          onPressed: snapshot.hasError == true ? null : (),
        );
      
    );
  

错误

Error: Bad state: Stream has already been listened to.
    at Object.throw_ [as throw] (http://localhost:52559/dart_sdk.js:5041:11)
    at _AsyncStreamController.new.[_subscribe] (http://localhost:52559/dart_sdk.js:33771:21)
    at _ControllerStream.new.[_createSubscription] (http://localhost:52559/dart_sdk.js:30842:46)
    at _ControllerStream.new.listen (http://localhost:52559/dart_sdk.js:30810:53)
    at new _SinkTransformerStreamSubscription.new (http://localhost:52559/dart_sdk.js:35828:36)
    at _BoundSinkStream.new.listen (http://localhost:52559/dart_sdk.js:35909:28)
at MappedListIterable.new.<anonymous> (http://localhost:52559/packages/rxdart/src/streams/combine_latest.dart.lib.js:118:29)
    at MappedListIterable.new.elementAt (http://localhost:52559/dart_sdk.js:22034:25)
    at ListIterator.new.moveNext (http://localhost:52559/dart_sdk.js:21840:55)
    at JsIterator.next (http://localhost:52559/dart_sdk.js:6709:21)
    at Function.of (http://localhost:52559/dart_sdk.js:43331:18)
    at MappedListIterable.new.toList (http://localhost:52559/dart_sdk.js:21653:26)
at http://localhost:52559/packages/rxdart/src/streams/combine_latest.dart.lib.js:137:88
    at Object._runGuarded (http://localhost:52559/dart_sdk.js:37591:7)
    at http://localhost:52559/dart_sdk.js:33785:17
    at _ControllerSubscription.new.[_guardCallback] (http://localhost:52559/dart_sdk.js:31204:9)
    at _SyncStreamController.new.[_subscribe] (http://localhost:52559/dart_sdk.js:33784:37)
    at _ControllerStream.new.[_createSubscription] (http://localhost:52559/dart_sdk.js:30842:46)
    at _ControllerStream.new.listen (http://localhost:52559/dart_sdk.js:30810:53)

【问题讨论】:

【参考方案1】:

使用Subject/broadcast StreamController 允许多个监听器

  final _email = PublishSubject<String>();
  final _pwd = PublishSubject<String>();

【讨论】:

此代码应放置在何处或如何提供帮助的任何说明?

以上是关于错误:错误状态:流已被收听的主要内容,如果未能解决你的问题,请参考以下文章

将序列化的 HttpResponseMessage 缓存到 Redis。读取错误。 “InvalidOperationException:流已被消耗。无法再次读取。”

包的现有状态已被丢弃

如何判断 pc.onnegotiationneeded 是不是因为流已被删除而被解雇?

使用绑定设备时的 gatt.writeDescriptor 错误状态回调

3 分钟无操作后,Firestore“流已删除”错误

HTTP-相应状态