app_center_connect 0.0.1 copy "app_center_connect: ^0.0.1" to clipboard
app_center_connect: ^0.0.1 copied to clipboard

PlatformAndroidiOS
unlisted

App Center analytics and crashes for Flutter

example/lib/main.dart

import 'dart:ui';

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

const androidSecret = '29dc151a-de62-445a-8d8a-6060d3e4f7be';
const iOSSecret = "ccd28e44-c9b4-4b0d-8f24-b487f3821b26";

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  runApp(const MyApp());

  // Start AppCenter
  await AppCenterConnect.start(
      appSecretAndroid: androidSecret, appSecretIOS: iOSSecret);

  // Log Flutter errors with FlutterError.onError
  FlutterError.onError = (FlutterErrorDetails errorDetails) {
    FlutterError.presentError(errorDetails);
    // Send Flutter errors to AppCenter
    AppCenterConnect.trackError(
      errorDetails.exceptionAsString(),
      properties: {"library": errorDetails.library ?? ""},
      stackTrace: errorDetails.stack,
    );
  };

  // Log errors not caught by Flutter with PlatformDispatcher.instance.onError
  PlatformDispatcher.instance.onError = (error, stack) {
    AppCenterConnect.trackError(
      error.toString(),
      stackTrace: stack,
    );
    return true;
  };

  await AppCenterConnect.configureAnalytics(enabled: true);

  await AppCenterConnect.configureCrashes(enabled: true);
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  void _trackError() {
    debugPrint("Sending error to AppCenter");
    throw const CustomException("Custom error");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: const Text('Plugin example app'),
      ),
      body: Container(
        padding: const EdgeInsets.all(20),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            const SizedBox(height: 8.0),
            FutureBuilder(
              future: AppCenterConnect.isCrashesEnabled(),
              builder: (_, AsyncSnapshot<bool?> snapshot) {
                if (snapshot.hasData) {
                  final isCrashesEnabled = snapshot.data!;

                  return Text('Crashes Enabled: $isCrashesEnabled');
                }

                return const CircularProgressIndicator.adaptive();
              },
            ),
            const SizedBox(height: 8.0),
            FutureBuilder(
              future: AppCenterConnect.isAnalyticsEnabled(),
              builder: (_, AsyncSnapshot<bool?> snapshot) {
                if (snapshot.hasData) {
                  final isAnalyticsEnabled = snapshot.data!;

                  return Text('Analytics Enabled: $isAnalyticsEnabled');
                }

                return const CircularProgressIndicator.adaptive();
              },
            ),
            const SizedBox(height: 8.0),
            ElevatedButton(
                onPressed: () => AppCenterConnect.trackEvent('SomeEvent'),
                child: const Text('Send Event')),
            ElevatedButton(
                onPressed: _trackError, child: const Text('Send Crash')),
          ],
        ),
      ),
    );
  }
}

class CustomException implements Exception {
  const CustomException(String message);
}