4

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();
      },
    );
  }
}
Lakshya Jain
  • 186
  • 1
  • 11

3 Answers3

5

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

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);
    }
  });
}
Yayo Arellano
  • 3,575
  • 23
  • 28
0

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);
Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147