Exception Handling in Flutter

Hasnain Mirrani
3 min readMay 16, 2023

Exception handling in Flutter is a crucial aspect of writing robust and reliable applications. Flutter provides various mechanisms to handle exceptions and errors that may occur during the execution of your app. Here are the main approaches for exception handling in Flutter:

1. Try-catch: This is the most common way to handle exceptions in Flutter. Wrap the code that you think may throw an exception in a `try` block, and catch the exception in a `catch` block. For example:

try {
// Code that may throw an exception
} catch (e) {
// Exception handling logic
}

2. On Error Callbacks: Some asynchronous operations in Flutter provide an `onError` callback, which allows you to handle exceptions or errors that occur during the execution of that operation. For example, the `Future` class has an `onError` parameter that you can use to handle exceptions:

Future<void> fetchData() async {
try {
// Code that may throw an exception
} catch (e) {
// Exception handling logic
}
}
void main() {
fetchData().catchError((e) {
// Exception handling logic
});
}

3. Error Zones: Flutter also provides an `ErrorWidget.builder` property, which allows you to define a global error handler for uncaught exceptions within your app. You can set this property in the `MaterialApp` widget to catch and handle any unhandled exceptions:

void main() {
ErrorWidget.builder = (FlutterErrorDetails details) {
// Exception handling logic
return Container(); // Return a custom error widget


runApp(MyApp());
}

4. Asynchronous Error Handling: When working with asynchronous code, you can use the `async` and `await` keywords to handle exceptions more conveniently. By using `try-catch` blocks with `await`, you can catch and handle exceptions in a more readable and sequential manner. For example:

Future<void> fetchData() async {
try {
// Code that may throw an exception
} catch (e) {
// Exception handling logic
}
}
void main() async {
try {
await fetchData();
} catch (e) {
// Exception handling logic
}
}

These are the primary techniques for exception handling in Flutter. Remember to analyze your application’s requirements and choose the most appropriate approach for each scenario. Additionally, Flutter provides specific exceptions and error classes for different scenarios, such as `FlutterError` or `AssertionError`, which you can utilize for more fine-grained exception handling.

try {
// Code that may throw an exception
} catch (e) {
// Exception handling logic
}

On Error Callbacks: Some asynchronous operations in Flutter provide an onError callback, which allows you to handle exceptions or errors that occur during the execution of that operation. For example, the Future class has an onError parameter that you can use to handle exceptions:

Future<void> fetchData() async {
try {
// Code that may throw an exception
} catch (e) {
// Exception handling logic
}
}

void main() {
fetchData().catchError((e) {
// Exception handling logic
});
}

Error Zones: Flutter also provides an ErrorWidget.builder property, which allows you to define a global error handler for uncaught exceptions within your app. You can set this property in the MaterialApp widget to catch and handle any unhandled exceptions:

void main() {
ErrorWidget.builder = (FlutterErrorDetails details) {
// Exception handling logic
return Container(); // Return a custom error widget
};

runApp(MyApp());
}

Asynchronous Error Handling: When working with asynchronous code, you can use the async and await keywords to handle exceptions more conveniently. By using try-catch blocks with await, you can catch and handle exceptions in a more readable and sequential manner. For example:

Future<void> fetchData() async {
try {
// Code that may throw an exception
} catch (e) {
// Exception handling logic
}
}

void main() async {
try {
await fetchData();
} catch (e) {
// Exception handling logic
}
}

These are the primary techniques for exception handling in Flutter. Remember to analyze your application’s requirements and choose the most appropriate approach for each scenario. Additionally, Flutter provides specific exceptions and error classes for different scenarios, such as FlutterError or AssertionError, which you can utilize for more fine-grained exception handling.

--

--

Hasnain Mirrani

Update the lattest and well explain All about Flutter make you from Zero to Hero in Flutter. follow https://www.linkedin.com/in/hasnain-mirrani-b47ab7131