Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DART: syntax of future then

Tags:

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 - The argument type 'void' cannot be assigned to the parameter type '(String) ->   dynamic' - Undefined name 'erg' - Expected to find ')'" 

2. myFuture(5).then( (erg) { callHandler(erg);}, onError: (e) => print (e)

What´s `onError: (e) => expr"` syntactically? 

3. Is there a difference between the onError: and the .catchError(e) variants?

like image 662
mica Avatar asked Dec 31 '13 17:12

mica


People also ask

How do you use the future in darts?

A future (lower case “f”) is an instance of the Future (capitalized “F”) class. A future represents the result of an asynchronous operation, and can have two states: uncompleted or completed. Note: Uncompleted is a Dart term referring to the state of a future before it has produced a value.

How do you use the future with then in Flutter?

Sometimes you don't want to turn the function into a Future or mark it async, so the other way to handle a Future is by using the . then function. It takes in a function that will be called with the value type of your Future. It's similar to a Promise in JavaScript without the resolve, reject explicitness.

What is then () in Flutter?

What is Async/Await/then In Dart/Flutter? await is to interrupt the process flow until the async method completes. then however does not interrupt the process flow. This means that the next instructions will be executed. But it allows you to execute the code when the asynchronous method completes.

What is Future object in Dart?

An object representing a delayed computation. A Future is used to represent a potential value, or error, that will be available at some time in the future. Receivers of a Future can register callbacks that handle the value or error once it is available.


1 Answers

1) The Fat Arrow is syntactic sugar for short anonymous functions. The two functions below are the same:

someFuture(arg).then((erg) => print(erg)); // is the same as someFuture(arg).then((erg) { return print(erg); }); 

Basically the fat arrow basically automatically returns the evaluation of the next expression.

If your callHandler2 has the correct signature, you can just pass the function name. The signature being that it accept the number of parameters as the future will pass to the then clause, and returns null/void.

For instance the following will work:

void callHandler2(someArg) { ... } // .. elsewhere in the code someFuture(arg).then(callHandler); 

2) See answer 1). The fat arrow is just syntactic sugar equivalent to:

myFuture(5).then( (erg){ callHandler(erg);}, onError: (e){ print(e); }); 

3) catchError allows you to chain the error handling after a series of futures. First its important to understand that then calls can be chained, so a then call which returns a Future can be chained to another then call. The catchError will catch errors both synchronous and asynchronous from all Futures in the chain. Passing an onError argument will only deal with an error in the Future its an argument for and for any synchronous code in your then block. Any asynchronous code in your then block will remain uncaught.

Recent tendency in most Dart code is to use catchError and omit the onError argument.

like image 175
Matt B Avatar answered Sep 18 '22 15:09

Matt B