Questions tagged [dart-async]

dart:async is a Dart library for asynchronous operations.

dart:async is a Dart library for asynchronous operations.

It supports streams, futures and allows to create timers.

314 questions
95
votes
2 answers

How do you get the current stacktrace in Dart for a Completer.CompleteException(exception, stackTrace);

If some code returns a future and determines that the future should return "Error" or "Exception" how can a stack trace be passed to Completer.completeException(exception, stackTrace);
adam-singer
  • 4,489
  • 4
  • 21
  • 25
81
votes
13 answers

is there any way to cancel a dart Future?

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…
Serge Tahé
  • 1,869
  • 2
  • 19
  • 20
69
votes
10 answers

Flutter - setState not updating inner Stateful Widget

Basically I am trying to make an app whose content will be updated with an async function that takes information from a website, but when I do try to set the new state, it doesn't reload the new content. If I debug the app, it shows that the current…
Jesús Martín
  • 1,293
  • 2
  • 11
  • 11
41
votes
2 answers

How do I do the equivalent of setTimeout + clearTimeout in Dart?

I'm porting some JavaScript to Dart. I have code that uses window.setTimeout to run a callback after a period of time. In some situations, that callback gets canceled via window.clearTimeout. What is the equivalent of this in Dart? I can use new…
Danny Tuppeny
  • 40,147
  • 24
  • 151
  • 275
26
votes
2 answers

DART: syntax of future then

I don´t understand the syntax of the then() clause. 1. myFuture(6).then( (erg) => print(erg) ) What´s (erg) => expr syntactically? I thougt it could be a function, but then( callHandler2(erg) doesn´t work, Error: "Multiple markers at this line -…
mica
  • 3,898
  • 4
  • 34
  • 62
25
votes
2 answers

Dart/Flutter - "yield" inside a callback function

I need to yield a list for a function; however, I want to yield the list from within a callback function, which itself is inside the main function - this results in the yield statement not executing for the main function, but rather for the callback…
Marcus Bornman
  • 375
  • 3
  • 7
25
votes
4 answers

Flutter BLoC pattern - How can I navigate to another screen after a stream event?

My question is about navigation used with the BLoC pattern. In my LoginScreen widget I have a button that adds an event into the EventSink of the bloc. The bloc calls the API and authenticates the user. Where in the LoginScreen Widget do I have to…
Sebastian
  • 3,666
  • 2
  • 19
  • 32
20
votes
7 answers

How to test an exception from a future

Continuing from yesterday question, how would I test that a async method throws an exception. main(){ test( "test2", () async { expect( await throws(), throwsException); }); } Future throws () async { throw new…
richard
  • 2,887
  • 5
  • 26
  • 36
18
votes
1 answer

The purpose of function `runZoned` of 'dart:async'

There is a special function runZoned provided by dart:async. The document is here: https://api.dartlang.org/docs/channels/stable/latest/dart_async.html#runZoned I'm not sure what's the purpose of this function, when will we need it, and how to use…
Freewind
  • 193,756
  • 157
  • 432
  • 708
17
votes
3 answers

Completer and Future in dart?

Future readData() { var completer = new Completer(); print("querying"); pool.query('select p.id, p.name, p.age, t.name, t.species ' 'from people p ' 'left join pets t on t.owner_id = p.id').then((result) { …
IBRIT
  • 385
  • 1
  • 3
  • 13
16
votes
2 answers

Getting values from Future instances

My data is something like this: { "five": { "group": { "one": { "order": 2 }, "six": { "order": 1 } }, "name": "Filbert", "skill": "databases" }, "four": { "group": { "three":…
Hesh
  • 413
  • 1
  • 3
  • 11
13
votes
1 answer

What's the difference between returning void vs returning Future?

Is there a difference between an async method that returns void, and one that returns Future? It seems that both are valid in Dart: void main() async { await myVoid(); await myFutureVoid(); } void myVoid() async { // Do…
Magnus
  • 17,157
  • 19
  • 104
  • 189
12
votes
6 answers

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

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…
Greg Sherman
  • 1,084
  • 1
  • 11
  • 22
12
votes
4 answers

Dart - how to mock a method that returns a future

I have a class that defines a method that returns a Future. The Future contains a list of class that also return a future. class User{ Future> albums(){ }; } class Album{ Future> photos(){ …
richard
  • 2,887
  • 5
  • 26
  • 36
1
2 3
20 21