Disable crashlytics for flutter web app and enable for flutter mobile app

Viewed 1629

I wanted to disable crashlytics for web and keep it enabled in android and iOS on my flutter app since firebase crashlytics is not supported by web. Can anyone tell me how I am supposed to do it. There is no Platform.isWeb so that's why I am confused. Please help me out and let me know. Here is my main.dart code for reference.

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      // Initialize FlutterFire
      future: Firebase.initializeApp(),
      builder: (context, snapshot) {
        // Firebase Crashlytics
        FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterError;

        // Check for errors
        if (snapshot.hasError) {
          return SomethingWentWrong();
        }

        // Show Application
        if (snapshot.connectionState == ConnectionState.done) {
          return StreamProvider<User>.value(
            initialData: null,
            value: AuthService().user,
            child: MaterialApp(
              debugShowCheckedModeBanner: false,
              home: Wrapper(),
            ),
          );
        }

        // Initialization
        return PouringHourGlassPageLoad();
      },
    );
  }
}
3 Answers

If you import

import 'package:flutter/foundation.dart';

there is a constant available called kIsWeb which you can use to initialize the crashlytics based on the platform.

something like

if(!kIsWeb) {
  initializeFlutterFire();
}

kIsWeb is the constant, which added inside foundation lib which tells whether the app is running on web or not.

import 'package:flutter/foundation.dart';

FirebaseCrashlytics.instance.setCrashlyticsCollectionEnabled(!kIsWeb);

The following fragment of code is how I made FirebaseCrashlytics initialize on iOS & Android but not on the Web.

void main() async {
  await runZonedGuarded(() async {
    WidgetsFlutterBinding.ensureInitialized();
    await Firebase.initializeApp();
    
    // If not web, setup Crashlytics
    if (!kIsWeb) {
      FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterFatalError;
    }
    
    runApp(const MyApp());
    
  }, (error, stackTrace) {
    // If not web, record the errors
    if (!kIsWeb) {
      FirebaseCrashlytics.instance.recordError(error, stackTrace, fatal: true);
    }
  });
}
Related