Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is wrong with this Dart error handler?

Tags:

flutter

dart

I'm doing some (I thought) basic exception handling in dart / flutter. I'm using the latest versions of dart and flutter as of last week (3/15/2019).

Here's my code:

void MyMethod() {     Storage.getFilePaths().then((paths) {       //do something     }).catchError((Exception error) {       //do something else       return null;     });  } 

However, when running the program and when an exception occurs I get this message below and can't see what the problem is?

'Invalid argument (onError): Error handler must accept one Object or one Object and a StackTrace as arguments, and return a a valid result: Closure: (Exception) => Null'

I assume I'm missing something silly, and would love to learn what that is.

like image 644
larryq Avatar asked Mar 20 '19 18:03

larryq


People also ask

How do you handle errors in darts?

If code within then() 's callback throws (as it does in the example above), then() 's Future completes with an error. That error is handled by catchError() . If myFunc() 's Future completes with an error, then() 's Future completes with that error. The error is also handled by catchError() .

What is exception handling error in Dart and flutter?

It is thrown when a schedule timeout happens while waiting for an async result. The main objective of the exception is to handle the run-time error and prevent the program from terminating abruptly. Every exception in the Dart is a subtype of the pre-defined class Exception.


1 Answers

}).catchError((Exception error) { 

has to be

}).catchError((Object error) { 

You can't limit to Exception here. Dart can throw all kinds of values.

like image 121
Günter Zöchbauer Avatar answered Sep 22 '22 21:09

Günter Zöchbauer