Sqljocky database query synchronously

I am trying to query a MySQL database synchronously with sqljocky. I have a Load class that launches a request and receives data, then imports the data into a User object. The problem I am facing is that the future that launches the request does not return in time to return the object to the calling method.

 DataObject user = new DataObject(); user.Get(1234); // I cannot do anything here because the returned object is null user.somethingElse(); DataObject Get(String ID){ return access.Load(this, ID); } // Runs query Load(DataObject object, String ID){ // Set up query String query = 'select * from users where ID = \'' + ID + '\''; // Run query var completer = new Completer.sync(); pool.query(query).then((result) { result.listen((row) { // Import into object object.Import(row); }, onDone: () { completer.complete(object); }); }); } 
+2
mysql dart
source share
1 answer

I assume you want to do something like this (note the unverified code):

 Future<DBObject> load(DBObject object, String id) { var query = "select ... where id='$id'"; // Note: check for SQL injection. return pool.query(query) .then((result) => result.toList()) .then((list) => list.forEach((row) => object.import(row))) .then((_) => object); } 

Have you read this article about using Futures?

The key point is that the method is asynchronous, and the calling code needs to wait for it to finish, then it should return a Future object (or a stream in some cases). There is no way in Dart to get the "block" function to wait for an asynchronous result. Feel free to ask some more questions in the comments.

+2
source share

All Articles