Error & Exception Handling in Flutter

Fredrick Mbugua
3 min readFeb 20, 2023

--

Image by storyset on Freepik

Flutter is a mobile application development framework that allows developers to build high-performance, visually appealing, and natively compiled applications for mobile, web, and desktop platforms. One important aspect of developing reliable Flutter applications is error handling. In this article, we will explore error handling in Flutter and Dart and provide some code samples to demonstrate best practices.

Error Handling in Dart

Dart is the programming language used by Flutter to build applications. Dart provides two types of errors: Exception and Error.

  • An Exception is an error that can be caught and handled by code.
  • An Error is an unrecoverable error that should not be caught and handled by code.

Dart’s Exception class has a subclass called FormatException. This exception is thrown when a string cannot be parsed to a number or date format. For example, if you try to parse a string that is not a valid date format, a FormatException will be thrown. You can catch this exception using a try-catch block like this:

try {
var date = DateTime.parse("2023-02-31");
} on FormatException catch (e) {
print("Date format is invalid.");
} catch (e) {
print("Unknown error occurred: $e");
}

In this example, we use a try-catch block to catch a FormatException when parsing a string to a DateTime object. If the exception is caught, we print an error message indicating that the date format is invalid. If another type of exception is caught, we print a message with the error details.

Error Handling in Flutter

Flutter provides many ways to handle errors, depending on the type of error and where it occurred in the application.

Handling Errors in Widgets

When an error occurs in a Flutter widget, the framework displays an error message on the screen. This can be useful during development, but it is not ideal for production applications. To handle errors in a widget, you can use the ErrorWidget.builder property. This property allows you to specify a custom widget that will be displayed instead of the default error message. Here's an example:

MaterialApp(
home: Scaffold(
body: ErrorWidget.builder((errorDetails) {
return Center(
child: Text(
'An error occurred. Please try again later.',
textAlign: TextAlign.center,
),
);
}),
),
);

In this example, we use the ErrorWidget.builder property to display a custom error message when an error occurs in the Scaffold widget.

Handling Errors in Future Operations

In Flutter, many operations are performed asynchronously using Futures. When an error occurs in a Future operation, the error is returned in the onError callback. Here's an example:

try {
await Future.delayed(Duration(seconds: 1));
throw Exception('Something went wrong');
} catch (e) {
print('Error: $e');
}

In this example, we use a try-catch block to catch an error that occurs during a delayed Future operation. We use the throw keyword to simulate an error. When the error is caught, we print a message with the error details.

Handling Errors in Streams

Streams are used to handle asynchronous data in Flutter. When an error occurs in a Stream, the error is returned in the onError callback. Here's an example:

final stream = Stream.fromFuture(Future.error('An error occurred'));

stream.listen(null, onError: (error) {
print('Error: $error');
});

In this example, we create a Stream from a Future that will throw an error. We then use the listenmethod to listen to the Stream and handle the error using theonError` callback.

Handling Uncaught Errors

In some cases, an error may occur that is not caught by any try-catch blocks or error handlers. In these cases, you can use the runZonedGuarded function to catch uncaught errors. Here's an example:

runZonedGuarded(() {
throw Exception('Something went wrong');
}, (error, stackTrace) {
print('Uncaught error: $error');
print(stackTrace);
});

In this example, we use the runZonedGuarded function to simulate an uncaught error. When the error occurs, the second parameter to the function is called with the error and stack trace.

Conclusion

In summary, error handling is an important part of developing reliable Flutter applications. Dart provides two types of errors, Exception and Error, and allows developers to catch and handle errors using try-catch blocks. In Flutter, errors can occur in widgets, Future operations, and Streams. To handle these errors, developers can use a combination of error handlers, error widgets, and the runZonedGuarded function. By understanding these error handling techniques, developers can write more reliable and maintainable Flutter applications.

--

--

Fredrick Mbugua

Software Developer @Safaricom. Experienced in Web, Mobile (Flutter & Kotlin), CrytpoCurrency, Smart Contracts, API and Software Design, Interaction Design