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.
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() .
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.
}).catchError((Exception error) {
has to be
}).catchError((Object error) {
You can't limit to Exception
here. Dart can throw all kinds of values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With