dart-async

Dart language support async/await programming style, or similar? [duplicate]

僤鯓⒐⒋嵵緔 提交于 2019-12-19 16:59:13
问题 This question already has an answer here : Async/Await feature in Dart 1.8 (1 answer) Closed 5 years ago . It is possible write similar code in Dart language? int i; try { i = await getResultAsync(); } catch(exception) { // Do something } 回答1: Basic support is already available. See https://www.dartlang.org/articles/await-async/ for more details. main() async { print(await foo()); try { print(await fooThrows()); } catch(e) { print(e); } } foo() async => 42; fooThrows() async => throw

DART server to send FUTURE function output back to the client, how?

人走茶凉 提交于 2019-12-13 02:25:52
问题 In a previous question here the server side dart file is calling a FUTURE email function that return either a confirmation msg or an error. the function below is working fine for the "print" function, but for the "res.write" is not working. the server.dart file: void handlePost(HttpRequest req) { HttpResponse res = req.response; print('${req.method}: ${req.uri.path}'); addCorsHeaders(res); req.listen((List<int> buffer) { SendConfirmationNote2Client(String msg) { print('msg: $msg'); // this is

Waiting for my class to initialize (or how to wait for a Future to complete)?

旧街凉风 提交于 2019-12-12 18:27:43
问题 Futures in Dart are the bane of my existence. I have a class, which calls an async (Future) function to start up the database instance like so: class DataManager { bool DbIsReady = false; Db _db; DataManager() { init_mongo_db(); } void init_mongo_db() { print("Initializing MongoDB"); _db = new Db("mongodb://127.0.0.1/test"); _db.open().then((_) { DbIsReady = true; }); } Future<List> attemptLogin(String username, String password) { users = _db.collection("users"); return // ... rest of code

Dart Event Queue: How to debug event queue?

只愿长相守 提交于 2019-12-12 14:55:14
问题 I'm writing some integration tests which utilize an HttpServer , a bunch of Directory().watch() 'ers and possibly some other future/stream listening code. I'm using the following test configuration: class LaserServerTestConfiguration extends SimpleConfiguration { LaserServer _server; LaserServerTestConfiguration(this._server); void onDone(bool success) { super.onDone(success); _server.stop(); } } And my tests look like this: main() { var conf = new LaserConfiguration.fromPath('test/test

How do I do this jquery pattern in dart? [duplicate]

人盡茶涼 提交于 2019-12-11 19:26:28
问题 This question already has an answer here : Waiting for Futures raised by other Futures (1 answer) Closed 5 years ago . I'm trying to convert the following javascript/jquery code into dart, but I'm having problems understanding how futures work. function fnA() { fnB().then(function() { // do something } } function fnB() { var ret = $.Deferred(); _db.open(database_name).then(function() { var defers = []; _db.keys().forEach(function(key_name) { var key_dfd = $.Deferred(); defers.push(key_dfd);

Testing dart ajax HttpRequest

与世无争的帅哥 提交于 2019-12-11 13:55:11
问题 I'm not quite sure I understand what's going on when I try testing post HttpRequest. Here's the code of my class that does the job: import 'dart:html'; class HttpReportAdapter { var logmaster; int log_level = 2; String url; HttpReportAdapter(this.url) {} post(r, level) { var data = { 'message' : r, 'log_level' : log_level.toString(), }; if(this.logmaster != null) data['log_level_string'] = this.logmaster.log_level_as_string(level); return HttpRequest.postFormData(this.url, data); } } An here

How to pass a callback function to a StreamController

被刻印的时光 ゝ 提交于 2019-12-11 05:02:06
问题 I was wondering something I'm creating a StreamController like that: class { StreamController _controller = new StreamController(onListen: _onListen(), onPause: _onPause(), onResume: _onResume(), onCancel: _onCancel()); Stream get stream => _controller.stream; } in an other class I invoke var sub = myInstance.stream.listen(null); and I'm really surprise that all the callbacks in the StreamController's constructor are triggered. Is there an explanation for this behavior ? Cheers ! 回答1: You

Dart: cancelable post delay / future

余生颓废 提交于 2019-12-11 04:56:19
问题 I am new to flutter and I want to translate some text from an InputField by calling an API. However I don't want to call it on every key stroke, but instead only when the user paused typing. On Android I would just use the Handler class with postDelay() with beforehand calling removeAllCallbacksAndMessages(null) . Is there a way to do something similar on Dart? Here is my current code: Future<String> getTranslation(String query, Language from, Language to) async { // cancel here if a call to

What is the difference between Stream<List<int>> and Stream<int> in Dart

别来无恙 提交于 2019-12-10 19:58:01
问题 I am trying to wrap my head around Dart Streams. In particular this example of the command line utility cat has the following lines of code: Stream<List<int>> stream = new File(path).openRead(); // Transform the stream using a `StreamTransformer`. The transformers // used here convert the data to UTF8 and split string values into // individual lines. return stream .transform(UTF8.decoder) .transform(const LineSplitter()) .listen((line) { if (showLineNumbers) { stdout.write('${lineNumber++} ')

Dart HTTP server and Futures

↘锁芯ラ 提交于 2019-12-10 17:44:01
问题 I am trying to write simple HTTP server which parse result of client.getUrl(). I've got everything working except I am not able to write back to http request object (print to console works fine). Relevant code is: main() { HttpServer.bind(InternetAddress.ANY_IP_V4, 4040) .then((HttpServer server) { print('listening on localhost, port ${server.port}'); server.listen((HttpRequest request) { Future loadedContent = loadUrl(furl); loadedContent.then((content) => print(content)); // loadedContent