dart-async

Dart Component: How to return result of asynchronous callback?

巧了我就是萌 提交于 2020-01-15 07:30:09
问题 Hey there I am quite new to Dart Futures and I have the following situation. Whenever a user types a letter in the UI the addressChanged() method in my ui_component is called. This method calls the method getProposals() in my maps componenet which does an asynchronous request to the google maps API. As soon as the results are here I want to return them to the UI Component which is going to populate the propasals dropdown in the UI. I am stuck with the last step: How (and whats the best way)

How can I execute two dart code in one HTML

这一生的挚爱 提交于 2020-01-01 06:12:12
问题 I'm trying to build an Dart App. This is the process that I would like to have. At the first connection, the user have a loading page. During this time, he has an animation, and in background, the big dart file is downloaded (came from dart2js for dart). Once it's over, the downloaded script is execute and the app cans start to work. Any idea about the possibility of this process ? Thank you. EDIT: import "dart:async"; @lazy import 'test.dart' as foo; const lazy = const DeferredLibrary('test'

Return multiple results from an async method?

天涯浪子 提交于 2019-12-30 13:36:19
问题 I currently have a method that looks something like this: typedef void MyCallback(int status, String body); void makeRequest(String url, MyCallback callback) async { if( someCondition ){ callback(1, ''); } Response response = await http.get(url); if( response.httpCode == 200 ){ callback(2, response.body); } else{ callback(3, ''); } } I want to get rid of the callback so that I can wait for the result(s) of makeRequest . But if I simply make it return a Future , I won't be able to return more

Return multiple results from an async method?

烈酒焚心 提交于 2019-12-30 13:36:03
问题 I currently have a method that looks something like this: typedef void MyCallback(int status, String body); void makeRequest(String url, MyCallback callback) async { if( someCondition ){ callback(1, ''); } Response response = await http.get(url); if( response.httpCode == 200 ){ callback(2, response.body); } else{ callback(3, ''); } } I want to get rid of the callback so that I can wait for the result(s) of makeRequest . But if I simply make it return a Future , I won't be able to return more

is there any way to cancel a dart Future?

隐身守侯 提交于 2019-12-28 02:53:07
问题 In a Dart UI, I have a button [submit] to launch a long async request. The [submit] handler returns a Future. Next, the button [submit] is replaced by a button [cancel] to allow the cancellation of the whole operation. In the [cancel] handler, I would like to cancel the long operation. How can I cancel the Future returned by the submit handler? I found no method to do that. 回答1: As far as I know, there isn't a way to cancel a Future. But there is a way to cancel a Stream subscription, and

Is Future's timeout method broken?

爱⌒轻易说出口 提交于 2019-12-24 05:25:15
问题 I have a long running task that I want to run asynchronously with a Future, but I also want it to timeout eventually. It seems to me that my timeout is never being called - but perhaps I am not using timeout correctly? // do actual solution finding asychronously Future populateFuture = new Future(() { populateGrid(words, gridWidth, gridHeight); }); populateFuture.timeout(const Duration(seconds: 3), onTimeout: () { window.alert("Could not create a word search in a reasonable amount of time.");

UnitTest example for asynchronous code

会有一股神秘感。 提交于 2019-12-24 02:18:42
问题 After reading the Unit Testing with Dart somehow I'm still can not understand how to use it with Future s. For example: void main() { group('database group',(){ setUp( () { // Setup }); tearDown((){ // TearDown }); test('open connection to local database', (){ DatabaseBase database = null; expect(database = new MongoDatabase("127.0.0.8", "simplechat-db"), isNotNull); database.AddMessage(null).then( (e) { expectAsync1(e) { // All ok } }, onError: (err) { expectAsync1(bb) { fail('error !'); } }

How do I create a blank Future in Dart + how do I return a future currently in progress?

穿精又带淫゛_ 提交于 2019-12-23 13:08:39
问题 I'm trying to create a server-side Dart class that performs various data-related tasks. All of these tasks rely on the database having been first initialized. The problem is that the init of the database happens asynchronously (returns a Future). I first tried to put the init code into the constructor, but have given up on this approach as it seems to not be viable. I am now attempting to figure out how to force the DB initialization as a first step in any method call that accesses data. So

Waiting for Futures raised by other Futures

♀尐吖头ヾ 提交于 2019-12-20 02:06:52
问题 I'm using the Lawndart library to access browser data, and want to collect the results of a set of queries. Here's what I thought should work: numberOfRecordsPerSection(callback) { var map = new Map(); db_sections.keys().forEach((_key) { db_sections.getByKey(_key).then((Map _section) { int count = _section.length; map[_key] = count; }); }).then(callback(map)); } However, when the callback is called, map is still empty (it gets populated correctly, but later, after all the Futures have

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

a 夏天 提交于 2019-12-19 17:01:16
问题 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