Introduction
No matter how carefully you write your code, errors will happen. A robust app must handle them gracefully, showing users a friendly message and logging the details for debugging. GetX provides the tools to build a centralised error handler that catches both Flutter‑specific errors and uncaught exceptions from your Dart code. This guide covers how to set up a global error handler that integrates with GetX's reactive state and dependency injection.
- Flutter's Error Handling Mechanisms
Flutter provides two main ways to catch errors:
FlutterError.onError– Catches errors from the Flutter framework (e.g., build errors).PlatformDispatcher.instance.onError– Catches uncaught Dart exceptions that occur outside the Flutter framework (e.g., async errors).- Zones – A more advanced way to isolate and handle errors in specific parts of your code.
- Creating a Global Error Service
Create a service that extends GetxService to centralise error handling. This service will register itself as the global error handler and expose a reactive variable to show errors in the UI.
- Registering the Service
Register the service as permanent in an initial binding. It will be available throughout the app.
- Displaying Errors in the UI
You can use the service's reactive variables to show a global error banner, or listen to errors and show snackbars via workers.
- Catching Errors in GetX Controllers
For async operations in controllers, always use try‑catch and update a reactive error variable. You can then use a worker to forward those errors to the global service.
- Handling Errors in Workers
Workers that perform async operations should also catch errors and forward them to the global service.
- Using Zones for Even More Control
If you want to catch errors in a specific part of your app (e.g., a particular widget subtree), you can use a Zone. This is useful for isolating errors and providing custom handling.
- Logging Errors to a Remote Service
In production, you should send errors to a logging service like Firebase Crashlytics or Sentry. Extend your error service to include this.
Best Practices
- Use a centralised error service – One place to handle logging and user feedback.
- Always catch exceptions in async code – Uncaught async errors will crash the app.
- Show user‑friendly messages – Avoid displaying technical stack traces.
- Log errors with context – Include screen name, user action, etc.
- Test error handling – Simulate errors and verify they are caught and displayed.
- Don't rely solely on global handlers – They are a safety net, but you should still handle expected errors locally.
Common Mistakes
- ❌ Not initializing the error service before
runApp– Global handlers may not be set. ✅ Set up the service in an initial binding and ensure it runs before any code that could error. - ❌ Swallowing errors without logging – Makes debugging impossible. ✅ Always log or forward errors to a central handler.
- ❌ Showing raw exception messages to users – May expose internal details. ✅ Map exceptions to user‑friendly strings.
- ❌ Forgetting to handle errors in workers and controllers – Only global handler catches uncaught; but if you catch locally, you must still log.
FAQ
- Q: Does GetX have a built‑in global error handler?
A: No, but it provides the tools to create one easily using Flutter's error handling APIs. - Q: How do I prevent the default Flutter error overlay from showing?
A: InFlutterError.onError, after handling, do not calldumpErrorToConsoleif you want to suppress the overlay. But for development, you might still want it. - Q: Can I catch errors from GetX navigation?
A: Yes, errors insideonPressedorGet.toare caught by the global handlers. For async navigation, use try‑catch. - Q: How to handle errors in isolated parts of the app?
A: UserunZonedGuardedaround those parts. This gives you fine‑grained control. - Q: Should I use
FlutterError.onErrororPlatformDispatcher.instance.onError?
A: Both. They cover different categories of errors. Use both for full coverage.
Conclusion
A global error handler is essential for production Flutter apps. By combining Flutter's error handling APIs with GetX's dependency injection and reactive state, you can build a system that not only logs errors but also provides a smooth user experience. Remember to test your error handling thoroughly and always provide a way for users to continue using the app after an error.