flutter_tcp_example icon indicating copy to clipboard operation
flutter_tcp_example copied to clipboard

Bad state: Stream has already been listened to.

Open val-creare opened this issue 4 years ago • 2 comments

Using this example as is and starting a connection, connection fails on tcp_bloc.dart line 40 : _socket = await _socketConnectionTask!.socket;. The error is Unhandled exception: Bad state: Stream has already been listened to. #0 _StreamController._subscribe (dart:async/stream_controller.dart:635:7) #1 _ControllerStream._createSubscription (dart:async/stream_controller.dart:786:19) #2 _StreamImpl.listen (dart:async/stream_impl.dart:473:9) #3 _Socket.listen (dart:io-patch/socket_patch.dart:2026:31).

I'm using Flutter 2.5.2 and Dart 2.14.3. Any hint on how to fix this issue?

@JulianAssmann

val-creare avatar Oct 15 '21 16:10 val-creare

You can try stream.toBroadcastStream()

JulianAssmann avatar Oct 15 '21 16:10 JulianAssmann

Thanks for your suggestion. I tried it and wasn't able to make it work. I'm shifting to a slightly different solution where I'm not using StreamSubscription and ConnectionTask. Instead, I'm defining _socket in the mapEventToState function as final _socket = await Socket.connect(YOUR_SERVER_IP, YOUR_SERVER_PORT);. Seems to work so far, but of course other things are not working now...

val-creare avatar Oct 15 '21 17:10 val-creare