Error Handling in Flutter
Tags: flutter
Published: 2024-02-12 03:10:30

---
title: Error Handling in Flutter
published: true
date: 2020-02-07 09:01:01 UTC
tags: flutter
canonical_url: https://medium.com/flutter-community/error-handling-in-flutter-98fce88a34f0
---

_How to handle errors in Flutter_

Handle your errors! Your app will have errors. You’ve got to handle them. Who wants to see the ‘Red Screen of Death?’ Certainly not your users. That’s bad. If it’s going to fail with an unrecoverable error, your app should be proactive about it. If it must crash, your app should make the effort to do so gracefully. It should even demonstrate some resilience and not crash too badly — not lose any data, etc. Your app should also be accountable — telling the user what just happened, and not just leaving them staring at a red screen. Like documentation, error handling seems to be the last thing we developers think about when developing software. That’s not good.

> Exceptions are considered conditions that you can plan ahead for and catch. Errors are conditions that you don’t expect or plan for.

> — [A tour of the core libraries](https://dart.dev/guides/libraries/library-tour#exceptions)

This article will cover Flutter’s error handling. We’ll walk through errors you will likely encounter when developing, and what Flutter does in response. Finally, we’ll cover how to incorporate your own error handling.

### Screenshots! Not Gists!

As always, I prefer using screenshots over gists to show code in my articles. I find them easier to work with, and easier to read. However, you can click/tap on them to see the code in a gist or in Github. Ironically, it’s better to read this article about mobile development on your computer than on your phone. Besides, we program mostly on our computers; not on our phones. For now.

### No Moving Pictures No Social Media

Note, there will be _gif_ files in this article demonstrating aspects of the topic at hand. However, it’s said viewing such _gif_ files is not possible when reading this article on platforms like Instagram, Facebook, etc. Please, be aware of this and maybe read this article on medium.com.

Let’s begin.

[![](https://cdn-images-1.medium.com/max/880/1*Dp8gTfNqkJfwL0v9oCZVsg.png)](https://medium.com/@greg.perry)<figcaption><a href="https://dev.to/andrious">Other Stories by Greg Perry</a></figcaption>

### Catcher in the Flutter

When I started using Flutter, one of the first things I researched was how it performed its error handling — knowing its importance. Doing so led me to the package, [Catcher](https://medium.com/flutter-community/handling-flutter-errors-with-catcher-efce74397862). Its author, [Jakub Homlala](https://medium.com/@jhomlala), wrote a wonderful [Dart package](https://pub.dev/packages/catcher) offering developers error handling for their apps. It presented developers with a list of pre-defined routines that would fire if and when an unrecoverable error occurred. Further, these options could vary depending upon whether your app was running in development or was running in production. It’s a very good Dart package. It’s certainly better than nothing!

[![](https://cdn-images-1.medium.com/max/154/1*bE3Mu6BdECVpJPzUwsuh4A.png)](https://pub.dev/packages/catcher)<figcaption><a href="https://pub.dev/packages/catcher">Catcher logo</a></figcaption>

### When In Error

We’ll come back to Jakub’s creation again, but for now, let’s step back and demonstrate what happens when Flutter encounters an error. We’ll use the ol’ standby example I’ve used in many of my past articles — the [counter](https://flutter.dev/docs/cookbook/testing/integration/introduction#1-create-an-app-to-test) starter app created by the ‘flutter create’ command. In your favorite IDE, when creating a new Flutter project, you’re likely presented with this ‘counter app’ as a starting point. It’s part of that app you see now below.

[![](https://cdn-images-1.medium.com/max/1024/1*yQLd9AV_uaYyvzBTpTMjdQ.png)](https://gist.github.com/Andrious/c239179abf27ee70aa4ec46405709691#file-counter_app_errors-dart-L97)

![](https://cdn-images-1.medium.com/max/682/1*rrnxvt1Uq3aViCk7wk6cxw.png)<figcaption><a href="https://gist.github.com/Andrious/c239179abf27ee70aa4ec46405709691#file-counter_app_errors-dart-L97">counter_app_errors.dart</a></figcaption>

You can see I’m ‘throwing’ an error when the “+” button is pressed. I know it’s not very imaginative, but you must admit, it’s such a simple app — there’s not a lot to work with here. Besides, it’s not the app we’re concerned with; it’s what Flutter does next in response to this blatant error.

Pressing that button initiates a ‘rebuild.’ The **setState** () function is called which means the **build** () function in the associating State object will also be called soon after. You can see, in the green inset below, that the state object is indeed called. Notice in the bigger screenshot the call to ‘build’ is in a **try-catch** statement. You’re going to see **try-catch** statements throughout the Flutter framework. You see, the framework does its best to anticipate where possible errors could occur. In this case, when an error does occur trying to build a widget, that error is caught in the **try-catch** statement, and the static function, [_ErrorWidget.builder_](https://api.flutter.dev/flutter/widgets/ErrorWidget/builder.html), is then called.

![](https://cdn-images-1.medium.com/max/1024/1*tQvaBchZDhGI8ksgngEygA.png)<figcaption><a href="https://github.com/flutter/flutter/blob/da0a7d8b2e3234f21c230a5424bf2569b9b28ca8/packages/flutter/lib/src/widgets/framework.dart#L4322">framework.dart</a></figcaption>

As stated in the ErrorWidget’s comments, “_when an error occurs while building a widget, the broken widget is replaced by the widget returned by this function._” The two screenshots below present this very function and the ‘red screen of you-know-what’ it returns. Tap on the screenshot, and you’ll be presented with the actual code in the Flutter framework.

[![](https://cdn-images-1.medium.com/max/1024/1*55fcPU4PTJ6WPsInuIIRKA.png)](https://github.com/flutter/flutter/blob/da0a7d8b2e3234f21c230a5424bf2569b9b28ca8/packages/flutter/lib/src/widgets/framework.dart#L4199)

![](https://cdn-images-1.medium.com/max/684/1*2CxSieCmvU9ZjpAuEXHmOw.png)<figcaption><a href="https://github.com/flutter/flutter/blob/da0a7d8b2e3234f21c230a5424bf2569b9b28ca8/packages/flutter/lib/src/widgets/framework.dart#L4199">framework.dart</a></figcaption>

### Bug Report

However, we’ve gotten ahead of ourselves a little bit. Let’s go back and take a look at what’s first passed to the ErrorWidget.builder as a parameter. What’s passed comes from the private function, **\_debugReportException** (). In the screenshot below, notice there are three positional parameters and one named parameter called, _informationCollector_, passed to the function, **\_debugReportException** ().

The first parameter is the function, **ErrorDescription** (), and it returns a [DiagnosticsNode](https://api.flutter.dev/flutter/foundation/DiagnosticsNode-class.html) object describing what was happening when the error occurred (Note, it provides more information when in Development mode than when in Release mode.). The next two parameters are an [Error](https://api.flutter.dev/flutter/dart-core/Error-class.html) object and a [StackTrace](https://api.flutter.dev/flutter/dart-core/StackTrace-class.html)object. The ‘error’ object could be anything frankly — however, in many cases, it’ll either be an [AssertionError](https://api.dart.dev/stable/2.7.1/dart-core/AssertionError-class.html) object or a [FlutterError](https://api.flutter.dev/flutter/foundation/FlutterError-class.html) object. Next, the StackTrace object lists the call sequence leading up to the error — conveying all the functions and class objects involved. It’s so to hopefully assist developers in correcting the issue.

[![](https://cdn-images-1.medium.com/max/1024/1*d0aq_k9TnGXr2OS1Y4D4DQ.png)](https://github.com/flutter/flutter/blob/da0a7d8b2e3234f21c230a5424bf2569b9b28ca8/packages/flutter/lib/src/widgets/framework.dart#L4321)<figcaption><a href="https://github.com/flutter/flutter/blob/da0a7d8b2e3234f21c230a5424bf2569b9b28ca8/packages/flutter/lib/src/widgets/framework.dart#L4321">framework.dart</a></figcaption>

The last parameter, the named parameter called, _informationCollector_, and it does just that. It collects even more information regarding the error. It’s, in fact, an [Iterable](https://api.flutter.dev/flutter/dart-core/Iterable-class.html) listing of DiagnosticsNode’s. It’s an expensive process ( a lot of memory cycles involved) and so the [**synchronous** generator](https://dart.dev/guides/language/language-tour#generators), _sync\*_, is used_._ It ‘lazily’ produces a sequence of values one at a time into the Iterable object.

### The Details

Let’s now take a peek inside the function, **\_debugReportException** (). At a glance, we see this function returns an object of type, [_FlutterErrorDetails_](https://api.flutter.dev/flutter/foundation/FlutterErrorDetails-class.html), to the static function, [_ErrorWidget.builder_](https://api.flutter.dev/flutter/widgets/ErrorWidget/builder.html)_._ Note, ErrorWidget.builder is of type:

```
Widget Function(FlutterErrorDetails details);
```

It returns a widget replacing the one that failed to build because of an error. But again, I’m getting a little ahead of myself. Back to the function, **\_debugReportException** (), and we see after producing the ‘exception’ object, _FlutterErrorDetails_, it calls the static function, [_reportError_](https://api.flutter.dev/flutter/foundation/FlutterError/onError.html), from the error class, [_FlutterError_](https://api.flutter.dev/flutter/foundation/FlutterError-class.html). See below.

[![](https://cdn-images-1.medium.com/max/1024/1*0yxZ2e0KmBkqNjiJ_pqQmQ.png)](https://github.com/flutter/flutter/blob/da0a7d8b2e3234f21c230a5424bf2569b9b28ca8/packages/flutter/lib/src/widgets/framework.dart#L5701)<figcaption><a href="https://github.com/flutter/flutter/blob/da0a7d8b2e3234f21c230a5424bf2569b9b28ca8/packages/flutter/lib/src/widgets/framework.dart#L5701">framework.dart</a></figcaption>

### Report The Error

Let’s take a look at this static function, _reportError_. You can see, with its assert statements, it insists it is passed a FlutterErrorDetails object with a non-null exception. It then checks if the static function of type, [FlutterExceptionHandler](https://api.flutter.dev/flutter/foundation/FlutterExceptionHandler.html), exists, and if so, calls it with the FlutterErrorDetails object as a parameter. Note, that _if_ statement tells you you could conceivably set that static function, _FlutterError.onError_, to null! This means you could literally ignore any errors in your app! Yeah, don’t do that.

[![](https://cdn-images-1.medium.com/max/1024/1*CR0lHe-Q0Nd-u5MHAYrvqw.png)](https://github.com/flutter/flutter/blob/da0a7d8b2e3234f21c230a5424bf2569b9b28ca8/packages/flutter/lib/src/foundation/assertions.dart#L714)<figcaption><a href="https://github.com/flutter/flutter/blob/da0a7d8b2e3234f21c230a5424bf2569b9b28ca8/packages/flutter/lib/src/foundation/assertions.dart#L714">assertions.dart</a></figcaption>

So, to review, you’ll find throughout the Flutter framework that the static function, _FlutterError.onError_, is called immediately prior to the static function, _ErrorWidget.builder_, with both using the same ‘exception’ object, _FlutterErrorDetails_. Pretty consistent too.

### Dump The Error

Again, by design, the static function, _FlutterError.onError_, is called whenever the Flutter framework catches an error. Its default behavior is to then call yet another static function, _dumpErrorToConsole_. Now guess what that does.

The big thing to come away with this, however, is that it is here where you could set your own function and override this default behavior. See where I’m going here? You can define your own ‘error handling’ by assigning a different ‘voidCallback’ function to the static function, _Flutter.onError_, of the type:

```
void Function(FlutterErrorDetails details)
```

Note, that it’s said if the error handler itself throws an exception, it will not be caught by the Flutter framework. There is a means to catch errors in the error handler. We’ll talk about that soon. For now, below you can see in the Flutter framework, the Flutter’s error handler is indeed assigned as default to dump errors to your IDE’s console in development and to your phone’s log files in production.

[![](https://cdn-images-1.medium.com/max/1024/1*etCU_GUGWZXrAn12K-7n3w.png)](https://github.com/flutter/flutter/blob/da0a7d8b2e3234f21c230a5424bf2569b9b28ca8/packages/flutter/lib/src/foundation/assertions.dart#L589)<figcaption><a href="https://github.com/flutter/flutter/blob/da0a7d8b2e3234f21c230a5424bf2569b9b28ca8/packages/flutter/lib/src/foundation/assertions.dart#L589">assertions.dart</a></figcaption>

### Dump The First Not The Rest

We’ll take a quick look at that static function, _dumpErrorToConsole_. Note, with its static variable, _\_errorCount_, it only records in detail the ‘first’ error to the console. Any further errors are usually given just a one-line summary only.

[![](https://cdn-images-1.medium.com/max/1024/1*7BA9-ZrbGABleZKrbtE9mw.png)](https://github.com/flutter/flutter/blob/da0a7d8b2e3234f21c230a5424bf2569b9b28ca8/packages/flutter/lib/src/foundation/assertions.dart#L626)<figcaption><a href="https://github.com/flutter/flutter/blob/da0a7d8b2e3234f21c230a5424bf2569b9b28ca8/packages/flutter/lib/src/foundation/assertions.dart#L626">assertions.dart</a></figcaption>

### The Console Knows

And so, with a press of that ‘+’ button, the following is recorded in the phone logs and depicted on the IDE’s console. It displays the exception message, however, it also gives you a link to the possible location of the error as well as a stack trace. All to correct the issue while in development.

![](https://cdn-images-1.medium.com/max/1024/1*hVEtODPvi-gXyxYvQCF7UA.png)

With that, your app is terminated, and it won’t continue. In production, the user is greeted with that red screen leaving them to hit the ‘back button’ on their phone, for example, to retreat back to the phone’s home screen. Done. You can quickly recap the process in [Handling errors in Flutter](https://flutter.dev/docs/testing/errors).

> “If the exception isn’t caught, the [isolate](https://dart.dev/guides/language/language-tour#isolates) that raised the exception is suspended, and typically the isolate and its program are terminated.” — [Exceptions](https://dart.dev/guides/language/language-tour#exceptions)

### To Catch An Error

Now let’s slap in Jakub’s [Catcher](https://pub.dev/packages/catcher) and see what happens now when we again press the ‘+’ button in our simple app example. His [readme](https://pub.dev/packages/catcher#basic-example) on pub.dev does a good job describing how to implement Catcher into your app. The screenshots below show how to do that as well as what happens when the ‘+’ button is finally pressed.

[![](https://cdn-images-1.medium.com/max/1024/1*pRD0kV7M6nJtZwBMulNjew.png)](https://gist.github.com/Andrious/c239179abf27ee70aa4ec46405709691#file-counter_app_errors-dart-L36)

![](https://cdn-images-1.medium.com/max/682/1*XXtQXLqyoZgdwKHrZPgYxQ.png)

![](https://cdn-images-1.medium.com/max/684/1*UNrfCPmkFilY1bNGQOnYoA.png)<figcaption><a href="https://gist.github.com/Andrious/c239179abf27ee70aa4ec46405709691#file-counter_app_errors-dart-L36">counter_app_errors.dart</a></figcaption>

Behind the scenes, an elaborate report is collected in an instance variable: _List<Report> \_cachedReports._ Catcher also presents you with a nice dialog window and a ‘non-red’ screen. There’s a typo on the screen, but that’s alright. As far as I know, English’s not Jakub’s first language. He’s from Poland, I believe. Further, he’s introduced Localization to Catcher, and so you’d be putting in your own messages anyway.

Yes, the Catcher presents you with a nice ‘non-red’ screen. However, Jakub did make that a separate option so that you can keep using the ‘red screen’ instead. I think I know why he did that. You see, he knows when a crash occurs, your app is likely now in an ‘unstable state’ all around. Who knows, displaying such a nice screen using Flutter’s standard widgets could cause a cascade of other errors. You see, standard widgets aren’t used when the static function, _ErrorWidget.build_, returns that red screen. I’ll explain that later too.

> “It is highly recommended that the widget returned from this function perform the least amount of work possible.” — [builder property](https://api.flutter.dev/flutter/widgets/ErrorWidget/builder.html)

First, let’s see what was done to ‘replace’ the default error handling. A quick glance at the heart of the Catcher package, and you’ll see how the custom error handling is introduced. It’s done in the function, **\_setupErrorHooks** (). Catcher defines what happens when there’s an error in the Flutter framework, what happens if there’s in the **main** () entry function, and even what happens if there’s an error in the ‘error handler’ itself.

The first thing we see is the old static function, _Flutter.onError_. It’s being assigned a new routine that calls the private function, **\_reportError** (). Right off the hop, you’ve got a custom error handler that will catch any errors that may occur in the Flutter framework itself.

The command, **_Isolate.current.addErrorListener_** _()_, is then called to catch any errors that may occur within the entry function, **main** () — in the ‘root zone’ where all Dart programs start. While the last function, **_runZoned_** (), is used to catch any errors in Dart code running ‘outside’ the Flutter framework — like Dart code inside the error handler itself for example.

[![](https://cdn-images-1.medium.com/max/1024/1*ezP8LVkRBQEuhQF83R6Xog.png)](https://github.com/jhomlala/catcher/blob/293d20a1c1ac8b10cd42fd50e2463c09bb5abcf7/lib/core/catcher.dart#L133)<figcaption><a href="https://github.com/jhomlala/catcher/blob/293d20a1c1ac8b10cd42fd50e2463c09bb5abcf7/lib/core/catcher.dart#L133">catcher.dart</a> *</figcaption>

### Build Widget Errors

Again, Catcher gives you the option to define a custom ‘error widget’ that would be displayed instead of the widget that fails to build. As you’ve seen, I assigned that option to the simple example app. See below.

You can also see Catcher then takes in the Flutter app itself as a parameter so to assign it to its own separate memory thread (Isolate) as well as set up its error handling. The additional parameters determine what sort of error handling is performed while in development and what is performed while in production. Very nice.

[![](https://cdn-images-1.medium.com/max/1024/1*LuJBQ932oqGrEAc-JwAUWQ.png)](https://gist.github.com/Andrious/c239179abf27ee70aa4ec46405709691#file-counter_app_errors-dart-L36)<figcaption><a href="https://gist.github.com/Andrious/c239179abf27ee70aa4ec46405709691#file-counter_app_errors-dart-L36">counter_app_errors.dart</a></figcaption>

And so, when we take a look inside the function, _addDefaultErrorWidget_, we see Catcher simply assigns an appropriate function to the static function, _ErrorWidget.builder,_ to return a custom widget if a build fails.

[![](https://cdn-images-1.medium.com/max/1024/1*AX5wINalaQneSyLCyc87Tg.png)](https://github.com/jhomlala/catcher/blob/293d20a1c1ac8b10cd42fd50e2463c09bb5abcf7/lib/core/catcher.dart#L384)<figcaption><a href="https://github.com/jhomlala/catcher/blob/293d20a1c1ac8b10cd42fd50e2463c09bb5abcf7/lib/core/catcher.dart#L384">catcher.dart</a> *</figcaption>

### Android Studio Also Handles Errors

While testing the Catcher error handler, for example, you may be perplexed as to why it is not working at first. If you’re using Android Studio, your IDE may be catching the errors instead. Be sure to go to Settings > Flutter and uncheck the option, ‘Show structured errors for Flutter framework issues’, before working on your error handling. Personally, it took me hours to discover that was the problem.

![](https://cdn-images-1.medium.com/max/1024/1*5Rh0w6Hqcjjy8G2RgtyJMw.png)

### Error by a Sliver

Let’s take a look at another common situation where errors may occur. It’s when you're making a scrolling ListView — a popular feature in all mobile apps. The tutorial, [Write your first Flutter app](https://flutter.dev/docs/get-started/codelab), has a perfect example with its ‘Startup Name Generator’ app. Its source code is on [Github](https://github.com/flutter/codelabs/blob/master/startup_namer/step8_themes/lib/main.dart). Of course, you could just tap the screenshot below for a copy.

[![](https://cdn-images-1.medium.com/max/1024/1*UXkK-OSVQZNPOpFhw4iTEw.png)](https://gist.github.com/Andrious/6168d68da03930bc6407e6634fa6571c#file-your_first_app-dart-L39)

![](https://cdn-images-1.medium.com/max/454/0*RooQjkSV_CS20-0H.gif)<figcaption><a href="https://gist.github.com/Andrious/6168d68da03930bc6407e6634fa6571c#file-your_first_app-dart-L39">Your_First_App.dart</a></figcaption>

The [SliverChildBuilderDelegate](https://api.flutter.dev/flutter/widgets/SliverChildBuilderDelegate-class.html) object is called to build the children for the listing (see below). Each index references a separate item to be listed in the viewport (the visible portion of the listing). Note, I’ve introduced an error to this sample app. It’s to be triggered at startup. However, because the making of this list is enclosed in a **try-catch** statement, the error is caught, and a widget is instead created from the private function, **\_createErrorWidget** (). You can see this private function is explicitly passed an Exception object and a StackTrace object.

[![](https://cdn-images-1.medium.com/max/1024/1*tgE3Zem9mY1B49zRlD2KbQ.png)](https://github.com/flutter/flutter/blob/da0a7d8b2e3234f21c230a5424bf2569b9b28ca8/packages/flutter/lib/src/widgets/sliver.dart#L439)<figcaption><a href="https://github.com/flutter/flutter/blob/da0a7d8b2e3234f21c230a5424bf2569b9b28ca8/packages/flutter/lib/src/widgets/sliver.dart#L439">sliver.dart</a></figcaption>

Inside the function, **\_createErrorWidget** (), you see both of our ‘Error Handlers’ are called one after the other (see screenshots below). Both are supplied the same FlutterErrorDetails object given the context description, ‘building.’ Pretty straightforward. And so, that’s the structure found throughout the Flutter framework, and consequently, the sequence of events if an error occurs:

> _An error-prone operation is called.  
> It’s inside a_ **_try-catch_** _statement.  
> An error calls a function in the_ **_catch_** _clause.  
> A FlutterErrorDetails object is created.  
> The FlutterError.reportError() is called.  
> The FlutterExceptionHandler, onError, is called.  
> Finally, the ErrorWidget.builder() is called._

Below, you see the ‘red screen’ is the result of this particular error.

[![](https://cdn-images-1.medium.com/max/1024/1*4jQ0rke_nKuZia740S76hA.png)](https://github.com/flutter/flutter/blob/da0a7d8b2e3234f21c230a5424bf2569b9b28ca8/packages/flutter/lib/src/widgets/sliver.dart#L1582)

![](https://cdn-images-1.medium.com/max/680/1*lR2S8_9f1BKtA31ET2KCXQ.png)<figcaption><a href="https://github.com/flutter/flutter/blob/da0a7d8b2e3234f21c230a5424bf2569b9b28ca8/packages/flutter/lib/src/widgets/sliver.dart#L1582">sliver.dart</a></figcaption>

By the way, it’s the [RenderErrorBox](https://api.flutter.dev/flutter/rendering/RenderErrorBox-class.html) class that’s used to create that wonder ‘Red Screen of Doom.’ Intentionally, there’s really not much to that class as it paints the error message using low-level functions. This is so to avoid depending on any widgets that may be in an ‘unstable state’ — after all, this class is called when the app has crashed and is likely unstable, so reporting the error should not create any more errors. The screenshot below is the portion of the RenderErrorBox class that defines its background color. Note, that it’s red when in development, but is a light gray color when in production.

[![](https://cdn-images-1.medium.com/max/1024/1*mV4mtnIUB59w3CtOgSMhAQ.png)](https://github.com/flutter/flutter/blob/da0a7d8b2e3234f21c230a5424bf2569b9b28ca8/packages/flutter/lib/src/rendering/error.dart#L95)<figcaption><a href="https://github.com/flutter/flutter/blob/da0a7d8b2e3234f21c230a5424bf2569b9b28ca8/packages/flutter/lib/src/rendering/error.dart#L95">error.dart</a></figcaption>

Under the Flutter.dev Cookbook, there are sections describing some of the error handling we’ve reviewed today. I’ve provided links here for your future reference: [Create a function to report errors](https://flutter.dev/docs/cookbook/maintenance/error-reporting#4-create-a-function-to-report-errors) and [Catch and report Dart errors](https://flutter.dev/docs/cookbook/maintenance/error-reporting#5-catch-and-report-dart-errors)

### TL;DR

Now if you’ve read my articles before, you know I like options. What developer doesn’t like options?! And so, of course, you know what I did. I wrote my own Error Handler. One with options.

If we go back to our first example, the ol’ starter Counter app, and comment out Catcher so to replace it with my little class, [_ErrorHandler_](https://gist.github.com/Andrious/6e3e568f510c6b84cdb38b178465829d#file-error_handler-dart). We’ll get the following when we again click on the ‘+’ button and cause an error:

[![](https://cdn-images-1.medium.com/max/1024/1*CM55EZ_aoCcKNsmyI48xbA.png)](https://gist.github.com/Andrious/c239179abf27ee70aa4ec46405709691#file-counter_app_errors-dart-L40)

![](https://cdn-images-1.medium.com/max/960/1*IqAFq4vpUDWZefkqEF9UbQ.gif)<figcaption><a href="https://gist.github.com/Andrious/c239179abf27ee70aa4ec46405709691#file-counter_app_errors-dart-L40">counter_app_errors.dart</a></figcaption>

Now it’s not as pretty as Catcher’s error screen, but that’s by design. You see, no ‘high-level’ widgets are used to display the error message. It’s painted on instead of using the [RenderBox](https://api.flutter.dev/flutter/rendering/RenderBox-class.html) class. I don’t want to cause any further errors. In development, I just want the error message and the stack trace. That’s it. The only other difference is you’re not seeing red anywhere.

Now that’s just its default behavior. This class has a constructor, of course, that allows you to assign your own ErrorWidget.builder function as well as your own FlutterError.onError exception handler. Make them as complex as you want if you must.

Catcher provides a little more ‘hand-holding’ and supplies pre-defined functionality. With my class, however, it’s all on you. You make up your own ‘error handling’ routines. Further, unlike Catcher, you would have to explicitly incorporate Localization and such into your routines.

Below is a screenshot of the class, _ErrorHandler_. Note, how the constructor saves the ‘current’ Exception handler and ErrorWidget builder and then sets any routines passed in as parameters. Note, that it’s a conventional class and not a static factory class allowing you to instantiate more than one instance of ErrorHandler. Now, why would you create more than one of this class?

[![](https://cdn-images-1.medium.com/max/1024/1*2N8ckeDtiUOaHaIBrccNEg.png)](https://gist.github.com/Andrious/6e3e568f510c6b84cdb38b178465829d#file-error_handler-dart-L58)<figcaption><a href="https://gist.github.com/Andrious/6e3e568f510c6b84cdb38b178465829d#file-error_handler-dart-L58">error_handler.dart</a></figcaption>

Further along in the class, you’ll see there’s the function, **dispose** (). Also, look at the cute little static getter called, _inDebugger_. That allows you to determine if you're in development or in production. You could use that in your own defined routines so to display one screen in development and display a different screen in production for example.

[![](https://cdn-images-1.medium.com/max/1024/1*T6XyPeDOx-fcFiZHOufKCA.png)](https://gist.github.com/Andrious/6e3e568f510c6b84cdb38b178465829d#file-error_handler-dart-L136)<figcaption><a href="https://gist.github.com/Andrious/6e3e568f510c6b84cdb38b178465829d#file-error_handler-dart-L136">error_handler.dart</a></figcaption>

### Crash In The Crash

But what is this all good for? Well, I’ll demonstrate. Looking back at the ‘Startup Name Generator’ example app, let’s incorporate Catcher and invoke the very same error at startup. The screenshots below show Cather’s ‘default Error Widget’ is being used and the result thereof. Notice when the crash occurs, the background is black. That’s because Catcher has had an error itself trying to display its error screen. You see, the app in such a state that additional widgets caused further errors. Well, let’s try something else. We’ll leave everything alone, but now introduce that ErrorHandler class as well.

[![](https://cdn-images-1.medium.com/max/1024/1*pTBdcnrcMr9DFMtwvaf0Jw.png)](https://gist.github.com/Andrious/6168d68da03930bc6407e6634fa6571c#file-your_first_app-dart-L11)

![](https://cdn-images-1.medium.com/max/686/1*2fOyCH1FKwCcDICzNXGqAQ.png)<figcaption><a href="https://gist.github.com/Andrious/6168d68da03930bc6407e6634fa6571c#file-your_first_app-dart-L11">Your_First_App.dart</a></figcaption>

Let’s use both Catcher and the ErrorHandler class. Inside the class, _RandomWordState_, we see it now has its own error handler instantiated when created and it’s cleared when the State object is itself disposed of. Now we’ll start up this app again and cause the error. See what happens now? We’ve got Catcher’s dialog window and ErrorHandler’s error screen. Conceivably, you could assign each State object in your app its own error handler if you want to catch specific errors in a specific fashion depending on the part of the app that’s in error. You can then save some data and close some low-level files, etc. Have error handling specific to that State object. Anyway, it’s an option.

[![](https://cdn-images-1.medium.com/max/1024/1*MBxj5A_1MZMpbGGm8kZulQ.png)](https://gist.github.com/Andrious/6168d68da03930bc6407e6634fa6571c#file-your_first_app-dart-L39)

![](https://cdn-images-1.medium.com/max/684/1*kxd0eMOiHTTWbNSiKWpLgA.png)<figcaption><a href="https://gist.github.com/Andrious/6168d68da03930bc6407e6634fa6571c#file-your_first_app-dart-L39">Your_First_App.dart</a></figcaption>

Again, it’s not pretty, but that simple error screen assures you that there are no further errors as a result of using a customized error handler. Frankly, even using a dialog window may be a little precarious. Of course, Catcher offers an array of options:

> _Dialog Report Mode_  
> - shows dialog with information about error.  
> _Page Report Mode_  
> -shows new page with information about error.  
> _Console Handler_  
> -is the default and shows crash log in console.  
> _Email Manual Handler_  
> - used to have the user to manually send email.  
> _Email Auto Handler_  
> -used to automatically send email with error reports.  
> _Http Handler_  
> -to send the error report to external server.  
> _File Handler_  
> -to store logs in file.  
> _Toast Handler_  
> -show a short message in the toast display.  
> _Sentry Handler_  
> - to send handled errors to the application monitoring platform, Sentry.io.  
> _Slack Handler_  
> -to send messages to your Slack workspace.  
> _Discord Handler_  
> -to send messages to your Discord workspace.

Anyway, the class, _ErrorHandler_, uses the very same classes as the default ‘ErrorWidget.builder’ routine to convey its default error message. Again, you’re free to assign your own ‘ErrorWidget.builder’ routine if you want to.

[![](https://cdn-images-1.medium.com/max/1024/1*7YKImOW7ZNAZMjz-qnEbFw.png)](https://gist.github.com/Andrious/6e3e568f510c6b84cdb38b178465829d#file-error_handler-dart-L230)

![](https://cdn-images-1.medium.com/max/682/1*jr2TiBlarNWIzKVQg0A4dQ.png)<figcaption><a href="https://gist.github.com/Andrious/6e3e568f510c6b84cdb38b178465829d#file-error_handler-dart-L230">error_handler.dart</a></figcaption>

Anyway! That class is yours to do what you like. Take it. Make it your own, and maybe share any changes you make — or don’t use it at all. Use Catcher! Use something! Those errors are coming, and you know it. No one wants to see that red screen. Especially, your users.

Cheers.

[Flutter Community](https://www.twitter.com/FlutterComm)

* * *