Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recommended way from dart team to catch errors when using await in Flutter

Tags:

flutter

dart

I write a lot of async code that uses await to handle Futures.

If I have

() async {
  var result = await someFuture();
}

what would be the preferred way to catch errors. Wraping the code in try/catch or doing

() async {
  var result = await someFuture().catch(_errorHandler);
}

EDIT:

Also, if I have many await calls in one async method, what is the preferred catch all the errors instead of writing .catchError for eachOne.

() async {
  var result = await someFuture();
  var result2 = await someFuture2();
  var result3 = await someFuture3();

}
like image 980
Tree Avatar asked Jun 03 '18 21:06

Tree


1 Answers

According to the Dart docs if you use await wrap it in a try-catch

like image 167
Thomas Avatar answered Oct 25 '22 09:10

Thomas