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?
1) The Fat Arrow is syntactic sugar for short anonymous functions. The two functions below are the same:
Basically the fat arrow basically automatically returns the evaluation of the next expression.
If your
callHandler2has 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 thethenclause, and returns null/void.For instance the following will work:
2) See answer 1). The fat arrow is just syntactic sugar equivalent to:
3)
catchErrorallows you to chain the error handling after a series of futures. First its important to understand thatthencalls can be chained, so athencall which returns aFuturecan be chained to anotherthencall. ThecatchErrorwill catch errors both synchronous and asynchronous from allFutures in the chain. Passing anonErrorargument will only deal with an error in theFutureits an argument for and for any synchronous code in yourthenblock. Any asynchronous code in yourthenblock will remain uncaught.Recent tendency in most Dart code is to use
catchErrorand omit theonErrorargument.