Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the difference between onError and CatchError in Dart

Tags:

flutter

dart

The only difference I see is that onError also gets the Stacktrace passed as a parameter. In which situations would you choose one other the other?

like image 862
Max Tromp Avatar asked May 09 '21 14:05

Max Tromp


People also ask

How do you use a Dart try catch?

The try / on / catch Blocks The on block is used when the exception type needs to be specified. The catch block is used when the handler needs the exception object. The try block must be followed by either exactly one on / catch block or one finally block (or one of both).

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.


1 Answers

Future<T> onError<E extends Object>(FutureOr<T> Function(E, StackTrace) handleError, {bool Function(E)? test})

Future<T> catchError(Function onError, {bool Function(Object error)? test});

onError is effectively a more precisely typed version of catchError.

So, with onError you can catch specific error types and specify a correctly typed error handler function, rather than all types and just a Function with catchError.

like image 137
davejlin Avatar answered Oct 22 '22 09:10

davejlin