1

I need to call dart function do something and return data back to aqueduct controller. So, Can I call any dart function inside aqueduct response controller? If yes, how?

class NtmsApiController extends Controller {
 @override
 Future<RequestOrResponse> handle(Request request) async {
   try {
           if (request.path.remainingPath != null) {
          _requestValue = request.path.remainingPath;
         …
         … can I go from here to dart function and get data back???? If yes, how?

UPDATE CODE: I have global variable and it prints in null. Once the socket gets the data the void _printResponse(String _response) prints the data and from there I assign the data to global variable. But in Future handle data become null so I cannot return the as a response object. Any idea?

@override
Future<RequestOrResponse> handle(Request request) async {
  _stopWatch = Stopwatch() //Global
    ..start();
  _response = ""; // Global

  if (request.path.remainingPath != null) {
    final _requestValue = request.path.remainingPath;

    await _getData(_requestValue);
  }

  print(_secureResponse); // It prints null, _secureResponse is Global variable
  return Response.ok("$_secureResponse")
    ..contentType = ContentType.json;
}

//TODO: GET DATA FROM CSBINS
Future<Null> _getData(String _request) async {
  await Socket.connect("192.168.22.120", 3000).then((Socket sock) {
    _socket = sock;
    _socket.write('$_request\r\n');
    _socket.listen (dataHandler,
            onError: errorHandler,
            onDone: doneHandler,
            cancelOnError: true);
  }).catchError((AsyncError e) {
    _response = "Server_Error";
  });

}

void dataHandler(data) {
  final List<int> byteArray = data;
  _response =  String.fromCharCodes(byteArray).trim();
}

void errorHandler(error, StackTrace trace) {
  _response = "Server_Error";
}

void doneHandler() {
  _socket.destroy();
}

void _printResponse(String _response) {

  // prints succefully ***************************
  print("$_response ... (${_stopWatch.elapsedMilliseconds} ms)");

  _secureResponse = _response;
  _stopWatch..stop();
  if (_stopWatch.isRunning == false) {
    _socket.flush();
    _socket.close();
    print("Socket Closed.");
  }
}
Nick
  • 4,163
  • 13
  • 38
  • 63
  • Can you give an example where you show your problem? There should not be any issue in calling any Dart function in the code you are showing. If you need to call a method on a object you need to have access to that object. – julemand101 Feb 04 '19 at 11:32
  • update the code and put some more detail and explain where I am having difficulty – Nick Feb 04 '19 at 14:45
  • But the result is null in your example? You are calling print(_secureResponse) which means you end up with the following in you _printResponse method: _printResponse = _printResponse; Since _printResponse is properly null when the program starts, you end up setting _printResponse to null. – julemand101 Feb 04 '19 at 15:36
  • I am not sure if I understan you correctly. The socket data handler gets the data and sens to printResponse method. The printResponse method prinst the data. which means socket receives the data fully and also in very fast mode. In printResponse I know there is a data from socket so I assign to secureResponse global variable. In future handle has await. Supose to wait and prints the acual secureResponse. My problem is I need to return data from socket handler back to Future handle on top. How do I do it? – Nick Feb 04 '19 at 17:13
  • Ok, I got confused with you code. Is it possible for you to upload a small complete example there shows what you problem is? In you current example you never actually call the _printResponse method so you first statement "The socket data handler gets the data and sens to printResponse method" is currently wrong. I think what you need is the Completer class: https://api.dartlang.org/stable/2.1.0/dart-async/Completer-class.html By using this class it is possible to return a Future and complete it with a value later. – julemand101 Feb 04 '19 at 19:15

0 Answers0