init static method

void init({
  1. required String url,
  2. required String apiKey,
  3. required String projectId,
  4. required void builder(),
  5. bool? debugMode,
  6. dynamic onError(
    1. ErrorModel details
    )?,
})

Initialize the Atelerix SDK

Sets up error monitoring and reporting. Does NOT initialize notifications. Call Atelerix.notifications.init() separately if you need push notifications.

Parameters:

  • url - Your project URL (e.g., 'https://api.atelerix.dev')
  • apiKey - Your project API key from dashboard
  • projectId - Your project ID from dashboard
  • builder - Function containing runApp(MyApp())
  • debugMode - Enable debug logging (default: false)
  • onError - Optional custom error handler

Example:

void main() {
  Atelerix.init(
    url: 'https://api.atelerix.dev',
    apiKey: 'your-api-key',
    projectId: 'your-project-id',
    debugMode: true,
    builder: () => runApp(MyApp()),
    onError: (error) {
      print('Custom error handler: ${error.message}');
    },
  );
}

Implementation

static void init({
  required String url,
  required String apiKey,
  required String projectId,
  required void Function() builder,
  bool? debugMode,
  Function(ErrorModel details)? onError,
}) {
  runZonedGuarded(() async {
    WidgetsFlutterBinding.ensureInitialized();

    try {
      // Step 1: Store configuration
      _keys.debug = debugMode ?? false;
      _keys.apiKey = apiKey;
      _keys.url = url;
      _keys.projectId = projectId;

      // Step 2: Retrieve app and device information
      await _package.getPackageDetails();
      await _native.getMobileDetails();

      // Step 3: Ping backend to register SDK
      await AtelerixInit().ping();

      // Step 4: Setup Flutter UI error handler
      FlutterError.onError = (FlutterErrorDetails details) {
        _errors.analyze(
          exception: details.exception.toString(),
          stack: details.stack ?? StackTrace.current,
        );
        if (onError != null && _errors.errorDetails != null) {
          onError(_errors.errorDetails!);
        }
      };

      // Step 5: Setup Dart platform error handler
      PlatformDispatcher.instance.onError = (error, stack) {
        if (_keys.debug) {
          debugPrint('[Atelerix] Platform error: $error\n$stack');
        }

        _errors.analyze(
          exception: error.toString(),
          stack: stack,
        );

        if (onError != null && _errors.errorDetails != null) {
          onError(_errors.errorDetails!);
        }

        return true; // Prevents app crash
      };

      // Step 6: Launch the app
      builder.call();
    } catch (e, stack) {
      if (_keys.debug) {
        debugPrint('[Atelerix] Initialization error: $e\n$stack');
      }
      // Still run the app even if init fails
      builder.call();
    }
  }, (Object error, StackTrace stack) {
    // Handle uncaught zone errors
    if (_keys.debug) {
      debugPrint('[Atelerix] Uncaught zone error: $error\n$stack');
    }

    _errors.analyze(
      exception: error.toString(),
      stack: stack,
    );

    if (onError != null && _errors.errorDetails != null) {
      onError(_errors.errorDetails!);
    }
  });
}