Open ciriousjoker opened 4 years ago
Thanks for the workaround, it was driving me nuts
Thank you so much for this workaround.
@aaahrens @ochmist Here's an improved version that also removes all the error messages (adapt as needed):
return {
setAnalyticsCollectionEnabled: () => null,
setCurrentScreen: () => null,
logEvent: () => null,
};
The previously proposed fixes are okay, however, they simply make the app not crash. Firebase Analytics will still be broken.
Here is a script that you can insert in your <body>
after loading at least firebase-app.js
. This is useful if you adhere to GDPR for example and asynchronously load Firebase analytics (say after the user consented via Google Tag Manager). Anyway, here is a mock that completely fixes the problem and allows loading Firebase Analytics later:
<!--
We need to create a mock for Firebase Analytics that ensures that it *does not matter **when***
the JS library is loaded. This is because Google Tag Manager will load firebase-analytics.js
and this 1. happens asynchronously and 2. only after the user consented.
The firebase.dart Dart library will crash if the firebase.analytics object does not exist,
which is why this is absolutely crucial for starting the app.
https://github.com/FirebaseExtended/firebase-dart/issues/335#issuecomment-797003830
-->
<script>
// This mock ensures that if the firebase_analytics Flutter plugin uses this mock as its
// instance (which does not change over time), the plugin will *still* be able to reach out
// to the actual firebase.analytics() instance because the object will be overridden once the
// firebase-analytics.js library is loaded in.
firebase.analytics = function () {
return {
mock: true,
app: function () {
var instance = firebase.analytics()
// Prevent infinite recursion if the real instance has not yet been loaded.
if (instance.mock === true) return
return instance.app
},
logEvent: function () {
var instance = firebase.analytics()
if (instance.mock === true) return
return instance.logEvent(...arguments)
},
setAnalyticsCollectionEnabled: function () {
var instance = firebase.analytics()
if (instance.mock === true) return
return instance.setAnalyticsCollectionEnabled(...arguments)
},
setCurrentScreen: function () {
var instance = firebase.analytics()
if (instance.mock === true) return
return instance.setCurrentScreen(...arguments)
},
setUserId: function () {
var instance = firebase.analytics()
if (instance.mock === true) return
return instance.setUserId(...arguments)
},
setUserProperties: function () {
var instance = firebase.analytics()
if (instance.mock === true) return
return instance.setUserProperties(...arguments)
},
}
}
</script>
Describe the bug A Flutter Web app will crash if the user has an adblocker enabled and
<script src="https://www.gstatic.com/firebasejs/.../firebase-analytics.js"></script>
can't be loaded. No widget is drawn, it crashes during the registerPlugin phase.To Reproduce Steps to reproduce the behavior:
flutter run web
Expected behavior App should still at least launch (even though analytics won't work obviously).
Additional context Temporary solution is to add this anywhere before loading main.js:
Flutter doctor Run
flutter doctor
and paste the output below:This issue was originally created here: https://github.com/FirebaseExtended/flutterfire/issues/3346