DEV Community

Cover image for Error Handling in Dart and Flutter
Stanislav Ilin
Stanislav Ilin

Posted on • Updated on

Error Handling in Dart and Flutter

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!


Error types in Dart

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


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 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.


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:

try {
  // your code here
} catch (e) {
  // handle the error here
Enter fullscreen mode Exit fullscreen mode

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:

assert(myValue != null, 'myValue cannot be null');
Enter fullscreen mode Exit fullscreen mode

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:

if (Platform.isIOS) {
  // handle iOS-specific errors here
} else if (Platform.isAndroid) {
  // handle Android-specific errors here
Enter fullscreen mode Exit fullscreen mode

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, which provides error tracking and monitoring for your application.

Talker main screen

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 of the package, there are settings available even from the application itself

Talker screens

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

Connect with me on GitHub and pls put ✨star✨ for the talker package.
It will be very pleasant for our team ❤️

Top comments (7)

opendataanalytics profile image
The Open Coder

I enjoyed reading your article on error handling in Dart and Flutter. You did a great job of explaining the differences between errors and exceptions and provided useful examples of how to handle them in your code. I particularly liked your recommendation of using third-party libraries like Talker for error tracking and monitoring. Keep up the good work!

easylive1989 profile image
Paul Wu

I can't find ErrorBoundary Widget. May I know what flutter version you used or any documents about this widget?

fawlid profile image

is error handling in dart and flutter the same?

ili profile image
iliya khoshnodi

I love flutter and dart, but I haven't time for learning it.
Are you making good money from flutter?

sajjadrahman56 profile image
Sajjad Rahman

It's possible. Depend on skill

pahrurozidev profile image

Are you from indonesian?

mosleim profile image
muhammad bukhori muslim

If someone need flutter developer, call me at otoritech