Skip to content

Instantly share code, notes, and snippets.

@tomerof
Created April 20, 2023 06:31
Show Gist options
  • Save tomerof/31cd672831abac0d540b8863dfdb9380 to your computer and use it in GitHub Desktop.
Save tomerof/31cd672831abac0d540b8863dfdb9380 to your computer and use it in GitHub Desktop.
Basic usage of catcher in Flutter application to handle errors and send them by email or by http request - like ACRA in android

To use the Catcher library in your Flutter application to catch errors and send them to a server, you can follow these steps:

  1. Add the Catcher dependency to your pubspec.yaml file and run flutter pub get to install it:

    dependencies:
      catcher: ^0.6.4
  2. Import the Catcher library in your Dart code:

    import 'package:catcher/catcher.dart';
  3. In your main function, set up a Catcher object and configure it to send error reports to your server:

    void main() {
      CatcherOptions debugOptions = CatcherOptions(DialogReportMode(), [ConsoleHandler()]);
      CatcherOptions releaseOptions = CatcherOptions(DialogReportMode(), [
        HttpHandler(
          HttpHandlerModel(
            Uri.parse('https://your-server.com/error-reports'),
            HttpMethod.post,
          ),
          printLogs: true,
        )
      ]);
    
      Catcher(
        rootWidget: MyApp(),
        debugConfig: debugOptions,
        releaseConfig: releaseOptions,
      );
    }

    In this example, Catcher is configured to use a DialogReportMode to show an error dialog to the user, and a HttpHandler to send error reports to a server at https://your-server.com/error-reports using HTTP POST requests. The printLogs: true parameter tells the HttpHandler to print debug logs to the console.

  4. Catch errors in your Flutter app using Catcher.reportCheckedError:

    try {
      // your code here
    } catch (error, stackTrace) {
      Catcher.reportCheckedError(error, stackTrace);
    }

    This will catch any errors that occur in your app and report them to the Catcher object, which will then send them to your server using the HttpHandler.

That's it! With these steps, you should be able to use the Catcher library to catch errors in your Flutter app and send them to a server for analysis.

To send the errors in the background without showing the user the error dialog, you can configure Catcher to use the SilentReportMode instead of the DialogReportMode. This will cause Catcher to automatically send error reports to the server without showing any dialogs to the user.

Here's an updated example that uses the SilentReportMode:

void main() {
  CatcherOptions debugOptions = CatcherOptions(DialogReportMode(), [ConsoleHandler()]);
  CatcherOptions releaseOptions = CatcherOptions(SilentReportMode(), [
    HttpHandler(
      HttpHandlerModel(
        Uri.parse('https://your-server.com/error-reports'),
        HttpMethod.post,
      ),
      printLogs: true,
    )
  ]);

  Catcher(
    rootWidget: MyApp(),
    debugConfig: debugOptions,
    releaseConfig: releaseOptions,
  );
}

In this example, Catcher is configured to use a SilentReportMode instead of a DialogReportMode. This will cause Catcher to automatically send error reports to the server without showing any dialogs to the user.

Note that you will still need to catch errors in your Flutter app using Catcher.reportCheckedError, as shown in the previous example:

try {
  // your code here
} catch (error, stackTrace) {
  Catcher.reportCheckedError(error, stackTrace);
}

With this configuration, any errors that occur in your app will be caught by Catcher and sent to your server in the background without any user interaction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment