crash_manager 1.0.2 copy "crash_manager: ^1.0.2" to clipboard
crash_manager: ^1.0.2 copied to clipboard

A comprehensive Flutter crash manager package with Firebase Crashlytics and Sentry support.

example/lib/main.dart

import 'dart:developer' as dev;

import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';

import 'crash_demo_page.dart';

/// Example main.dart showing proper Clean Architecture setup
///
/// This demonstrates the CORRECT approach where:
/// 1. Firebase is initialized at the APPLICATION LEVEL
/// 2. No services handle their own initialization
/// 3. Clean separation of concerns
void main() async {
  // Ensure Flutter is initialized
  WidgetsFlutterBinding.ensureInitialized();

  try {
    // STEP 1: Initialize Firebase at APPLICATION LEVEL
    // This is the ONLY place where Firebase should be initialized
    // Services should NEVER handle their own initialization
    await Firebase.initializeApp();

    dev.log('✅ Firebase initialized successfully at application level');

    // STEP 2: Run the app
    // All services will receive already-initialized Firebase instances
    runApp(const MyApp());
  } catch (e) {
    dev.log('❌ Failed to initialize Firebase: $e');

    // Show error to user
    runApp(
      MaterialApp(
        home: Scaffold(
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                const Icon(Icons.error, size: 64, color: Colors.red),
                const SizedBox(height: 16),
                const Text(
                  'Firebase Initialization Failed',
                  style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
                ),
                const SizedBox(height: 8),
                Padding(
                  padding: const EdgeInsets.all(16),
                  child: Text(
                    'Please ensure you have:\n'
                    '• Android: google-services.json in android/app/\n'
                    '• iOS: GoogleService-Info.plist in ios/Runner/\n\n'
                    'Error: $e',
                    textAlign: TextAlign.center,
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Crash Manager Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const CrashDemoPage(),
    );
  }
}

/// 🏆 CLEAN ARCHITECTURE BENEFITS DEMONSTRATED:
///
/// 1. **Single Responsibility**: main.dart only handles app initialization
/// 2. **Separation of Concerns**: Firebase init separated from crash management
/// 3. **Dependency Inversion**: Services receive dependencies, don't create them
/// 4. **Easy Testing**: Can mock Firebase for testing
/// 5. **Clear Error Handling**: Initialization errors handled at app level
/// 6. **Maintainability**: Changes to Firebase setup don't affect services
///
/// ❌ ANTI-PATTERNS AVOIDED:
/// - Services initializing their own dependencies
/// - Factory methods with complex initialization logic
/// - Mixed concerns in service constructors
/// - Hard-to-test initialization code
/// - Tight coupling between services and external dependencies