Error Handling in Dart and Flutter
Tags: flutter, dart, mobile, android
Published: 2023-02-18 13:33:10

Hi everyone! I won't even tell you how important to handle all errors and exceptions in your Flutter application.
**Let's start right now with the facts!**

![Friends](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fzzyme29rysto68nmzvq.gif)


## Error types in Dart
We have two basic error types in Flutter. 
**Error and Exception**. But what's the difference?


![Spidermans](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x0i0lp2hss7ipga7l4ii.png)



In dart, an "error" is a runtime problem that can't be handled by the program, while an "exception" is a runtime problem that can be handled by the program.

### Errors
Errors are typically caused by conditions outside the control of the program, such as hardware failure or operating system issues, and they are not expected to be handled by the program. Examples of errors in Dart include OutOfMemoryError or StackOverflowError. When an error occurs, the program will usually terminate.

### Exceptions
On the other hand, exceptions are caused by conditions within the program's control, such as invalid user input or network errors. Exceptions can be caught and handled by the program using a try-catch block, allowing the program to recover from the error and continue execution. Examples of exceptions in Dart include FormatException or SocketException.

In summary, errors are problems that can't be handled by the program, while exceptions are problems that can be handled by the program. It's important to differentiate between the two when developing your Dart applications and handling runtime problems.

## How to escape them?

**1. Use Try-Catch Blocks**

Try-catch blocks are a fundamental aspect of handling errors in any programming language, including Flutter. They allow you to handle exceptions that may occur during the execution of your code. When you use a try-catch block in your application, you can catch exceptions and handle them appropriately. For example, you may want to display an error message to the user or log the error for debugging purposes.

Here's an example of how you can use try-catch blocks in your Flutter application:

```dart
try {
  // your code here
} catch (e) {
  // handle the error here
}
```

**2. Use assert**

Assert is a tool that can be used to validate assumptions about the state of your application during development. If an assertion fails, an exception is thrown, which can help you catch and debug errors early on. You can use assert statements to check whether your code is behaving as expected and to identify errors before they become problematic.

Here's an example of how you can use assert in your Flutter application:

```dart
assert(myValue != null, 'myValue cannot be null');
```

**3. Handle Platform-Specific Errors**

If your Flutter application targets multiple platforms, handling platform-specific errors. For example, on iOS, you may need to handle errors related to permissions, while on Android, you may need to handle errors related to device compatibility. You can ensure that your application behaves consistently across different platforms by handling platform-specific errors.

Here's an example of how you can handle platform-specific errors in your Flutter application:

```dart
if (Platform.isIOS) {
  // handle iOS-specific errors here
} else if (Platform.isAndroid) {
  // handle Android-specific errors here
}
```

**4. Use Third-Party Libraries**
There are several third-party libraries available for Flutter that can help you handle errors in your application. 

One popular library is [Talker](https://github.com/Frezyx/talker), which provides error tracking and monitoring for your application.


![Talker main screen](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qcbl8kktf20abgd8g9cy.png)

The talker library was designed for saving your time with error-handling process. The main concept of talker is to collect errors and redirect them to the right pipeline. 

For example, something errors must be shown to user while others don't. You can configure this logic very simply using talker. Some errors should be reported to your tracking system, while others should not. 

For such cases, in [the latest update](https://github.com/Frezyx/talker/releases/tag/2.2.0) of the package, there are settings available **even from the application** itself

![Talker screens](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p1vquv5za7eae5xi3c4s.png)

In order to spend even less your time - we have developed add-ons for some popular libraries, such as dio and BLoC. And we continue to develop this idea.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/voax23jeds8pncy3uf40.png)

Connect with me on GitHub and pls put ✨star✨ for the [talker](https://github.com/Frezyx/talker) package. 
It will be very pleasant for our team ❤️