简体   繁体   中英

Future functions in DART working with ORACLE DART pub

I'm using oracledart pub , and need to get the results returned as Map to the main function, I know it is a FUTURE function, and read about FUTURE, but looks still not clear for me, or I'm doing something wrong in my code, my function is as below:

void main() {
     var ORAresults = <Map>[];

     ORA()
          .then((results) => ORAresults = results) 
          .catchError((e) => 'Sorry, something wrong!'); 
}


ORA() {
    var results = <Map>[];
   connect(
               "SYSTEM","pswd",
               "(DESCRIPTION="
               "(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))"
               "(CONNECT_DATA=(SERVICE_NAME=XE)(SERVER=DEDICATED)))")
   .then(  
          (oracleConnection) {
          var resultset = oracleConnection.select("select * from vendors");

          while(resultset.next()) {
               results.add({"code":"vCode 1","name": "${resultset.getStringByName('NAME')}"});
            } 

      print('the results inside  $results');  // this works very well
      return results;
   }, 
   onError: (error) {
          print("Failed to connect: $error");
   });
}

When I run the above, I get this error:

  Breaking on exception: object of type NoSuchMethodError

the file dart:core-patch_object_patch.dart is opening, and pointing to:

  noSuchMethod(Invocation invocation) {
   =>   return _noSuchMethod(invocation.isMethod,        // this line is being highlighted!
        internal.Symbol.getName(invocation.memberName),
        invocation._type,
        invocation.positionalArguments,
        _symbolMapToStringMap(invocation.namedArguments));
    }

I thing the error is due to something wrong here, because if I removed these lines, the error disappear.:

         ORA()
          .then((results) => ORAresults = results) 
          .catchError((e) => 'Sorry, something wrong!');

any help pls.

Your ORA() function does not return the Future it uses. Change the connect( line to return connect( , and it should work.

When you do ORA().then(...) , you're using ORA() 's return value as a Future , but your ORA() function returns null (it has no return statement, so it returns null by default). What you really want to do is return the Future you're building on with the connect() .

Thanks @Tonio and @Robert, I think now I understood the meaning of the FUTURE better :)

I was able to solve the issue, based on your hints and explanations, as below:

in the server.dart

void handlePost(HttpRequest req) {
HttpResponse res = req.response;
     switch (req.uri.path) {
            ...
            case '/getVendors':
              getVendors(req);
              break;
            default:
              break;
           }     
   }

void getVendors(HttpRequest req) {
HttpResponse res = req.response;
addCorsHeaders(res);
print('${req.method}: ${req.uri.path}');

var vendors = <Map>[];
connect(
    "SYSTEM",
    "pswrd",
    "(DESCRIPTION="
      "(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))"
      "(CONNECT_DATA=(SERVICE_NAME=XE)(SERVER=DEDICATED)))")
.then(  
  (oracleConnection) {
     var resultset = oracleConnection.select("select * from vendors");

     while(resultset.next()) {
        vendors.add({"code":"${resultset.getStringByName('CODE')}","name": "${resultset.getStringByName('NAME')}"});
      } 
      res.write(JSON.encode(vendors));
      res.close(); 
  }, 
  onError: (error) {
    print("Failed to connect: $error");
  });
}

and in the client.dart

 main(){
           HttpRequest request;  
           String serverResponse = '';  
    ...
 }
 void submit(){
     request = new HttpRequest();
     request.onReadyStateChange.listen(onData_getvendors); 

     var url = 'http://127.0.0.1:8004/getVendors';  
     request.open('POST', url);
     request.send('');
  } 

  onData_getvendors(_){ 
      if (request.readyState == HttpRequest.DONE && request.status == 200) {   // Data saved OK.                                
      for(Map vendor in JSON.decode(request.responseText)){
          vendor..children.add(new OptionElement(value: vendor['code'], data: vendor['name']));
       } 
      else if (request.readyState == HttpRequest.DONE &&
               request.status == 0) {         // Status is 0...most likely the server isn't running.
              serverResponse=request.responseText;
           }
   } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM