Flutter Native Crash Log

A Flutter plugin to capture native Android crashes, log non-fatal errors, and view or share them within the app.

Features

  • Capture Native Crashes: Automatically catches uncaught exceptions in the Android native layer.
  • Persistent Logs: Logs are saved locally using SharedPreferences.
  • Log Viewer: Built-in UI to view crash details (Timestamp, Error, Stack Trace).
  • Share Logs: Export logs as a JSON file using the system share sheet.
  • Non-Fatal Error Logging: Log "soft crashes" or handled exceptions without killing the app.
  • Force Crash: Test the crash reporting by forcing a native crash.

Installation

Add the following to your pubspec.yaml:

dependencies:
  flutter_native_crash_log:
    path: ./ # Or git url

Usage

1. Initialize

Initialize the plugin in your main.dart, preferably after the first frame ensures the context is available for the overlay.

import 'package:flutter_native_crash_log/flutter_native_crash_log.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});
  
  // ...
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
    // Initialize after build
    WidgetsBinding.instance.addPostFrameCallback((_) {
      // trackLog: true enables the native handler and shows the floating debug button
      FlutterNativeCrashLog.initialize(context, trackLog: true);
    });
  }
}

2. Force a Crash (For Testing)

To verify that native crashes are being caught, you can force a crash:

ElevatedButton(
  onPressed: () {
    FlutterNativeCrashLog.forceCrash(); // App will close
  },
  child: const Text('Force Native Crash'),
),

3. Log a Non-Fatal Error

To log an important event or error without crashing the app:

try {
  // critical operation
} catch (e, stack) {
  FlutterNativeCrashLog.logNonFatalError(e.toString(), stackTrace: stack.toString());
}

4. Share Logs

The built-in log viewer has a share button, but you can also trigger it programmatically:

await FlutterNativeCrashLog.shareCrashLogs();

Viewing Logs

  1. If trackLog: true was passed to initialize, a floating bug icon will appear on the screen.
  2. Tap the icon to open the Crash Logs screen.
  3. Tap on any log entry to expand and view the stack trace.
  4. Use the Share icon in the AppBar to export logs.