Search code examples
flutterdartcrashlyticsflutter-webincompatibility

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


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();
      },
    );
  }
}

Solution

  • 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();
    }