Dart: Bad state: Stream has already been listened to

  android, dart, flutter, sockets, tcp

I have very easy TCP client:

Socket socket = await Socket.connect(ipAddress, port);


socket.add(utf8.encode('Text 1'));
socket.listen((List<int> event) {
      var x = utf8.decode(event)
      //do something with this value...
});


socket.add(utf8.encode('Text 2'));
socket.listen((List<int> event) {
      var x = utf8.decode(event)
      //do something with this value...
});

.....
.....
.....

socket.close();

Server waits for new data from client("Text 1", "Text 2"….), proces those data and then returns answer. How can I make dart to listen to all of those answers? Fist listen() works but the second throws error: Unhandled Exception: Bad state: Stream has already been listened to.

Here is complete error message:

[ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: Bad state: Stream has already been listened to.
E/flutter ( 5303): #0      _StreamController._subscribe (dart:async/stream_controller.dart:635:7)
E/flutter ( 5303): #1      _ControllerStream._createSubscription (dart:async/stream_controller.dart:786:19)
E/flutter ( 5303): #2      _StreamImpl.listen (dart:async/stream_impl.dart:473:9)
E/flutter ( 5303): #3      _Socket.listen (dart:io-patch/socket_patch.dart:2004:31)
E/flutter ( 5303): #4      serverRequest.<anonymous closure> (package:vivadispecing_sofer/scripts.dart:602:14)
E/flutter ( 5303): #5      _rootRunUnary (dart:async/zone.dart:1362:47)
E/flutter ( 5303): #6      _CustomZone.runUnary (dart:async/zone.dart:1265:19)
E/flutter ( 5303): #7      _CustomZone.runUnaryGuarded (dart:async/zone.dart:1170:7)
E/flutter ( 5303): #8      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341:11)
E/flutter ( 5303): #9      _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
E/flutter ( 5303): #10     _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:733:19)
E/flutter ( 5303): #11     _StreamController._add (dart:async/stream_controller.dart:607:7)
E/flutter ( 5303): #12     _StreamController.add (dart:async/stream_controller.dart:554:5)
E/flutter ( 5303): #13     _Socket._onData (dart:io-patch/socket_patch.dart:2144:41)
E/flutter ( 5303): #14     _rootRunUnary (dart:async/zone.dart:1370:13)
E/flutter ( 5303): #15     _CustomZone.runUnary (dart:async/zone.dart:1265:19)
E/flutter ( 5303): #16     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1170:7)
E/flutter ( 5303): #17     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341:11)
E/flutter ( 5303): #18     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
E/flutter ( 5303): #19     _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:733:19)
E/flutter ( 5303): #20     _StreamController._add (dart:async/stream_controller.dart:607:7)
E/flutter ( 5303): #21     _StreamController.add (dart:async/stream_controller.dart:554:5)
E/flutter ( 5303): #22     new _RawSocket.<anonymous closure> (dart:io-patch/socket_patch.dart:1680:33)
E/flutter ( 5303): #23     _NativeSocket.issueReadEvent.issue (dart:io-patch/socket_patch.dart:1192:14)
E/flutter ( 5303): #24     _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
E/flutter ( 5303): #25     _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)

Source: Android Questions

LEAVE A COMMENT