How to return socket data from Future in Dart? How to return socket data from Future in Dart? dart dart

How to return socket data from Future in Dart?


This might work. I think you should read up about streams/futures/async/await, though.

Future<String> _handle(String _request) async {  print("Future Starts Here");  print("Request: $_request");    _errorData = "Server_Error";    if (_request != null) {      print("Request has data");      // =============================================================      Socket _socket;      await Socket.connect("192.168.22.120”, 3000).then((Socket sock) {        _socket = sock;      }).then((_) {        print("Socket sends data to database");        // SENT TO SERVER ************************        _socket.write('$_request\r\n');        return _socket.first;      }).then((data) {        print("Socket start to listen to database");        // GET FROM SERVER *********************        _secureResponse =  new String.fromCharCodes(data).trim();        print("(1) $_secureResponse");      }).catchError((AsyncError e) {        _secureResponse = _errorData;        print("(3) $_secureResponse");        exit(1);      });      // ==============================================================    } else {      _secureResponse = _errorData;      print("(4) $_secureResponse");    }  print("(6) $_secureResponse");  print("Future Ends Here");  return _secureResponse;}