Flutter Web 错误 TypeError: dart.global.firebase.analytics is not a function

Posted

技术标签:

【中文标题】Flutter Web 错误 TypeError: dart.global.firebase.analytics is not a function【英文标题】:Flutter Web error TypeError: dart.global.firebase.analytics is not a function 【发布时间】:2021-08-01 08:36:42 【问题描述】:

我正在使用以前创建的 firebase 移动应用程序,该应用程序在移动级别与 firebase 完全兼容,但是,当我尝试将其作为 chrome 页面运行时,它似乎不起作用,并且在编译时会触发列出的错误在标题中。 Flutter Doctor 显示一切正常,并且 chrome 是列出的设备。尝试构建网页时,Chrome 窗口中会出现一个空白屏幕。

TypeError: dart.global.firebase.analytics is not a function
    at Object.analytics$ [as analytics] (http://localhost:59823/packages/firebase/src/top_level.dart.lib.js:114:102)
    at new firebase_analytics_web.FirebaseAnalyticsWeb.new (http://localhost:59823/packages/firebase_analytics_web/firebase_analytics_web.dart.lib.js:70:64)
    at Function.registerWith (http://localhost:59823/packages/firebase_analytics_web/firebase_analytics_web.dart.lib.js:29:82)
    at Object.registerPlugins (http://localhost:59823/packages/reminder_application/generated_plugin_registrant.dart.lib.js:23:49)
    at main (http://localhost:59823/web_entrypoint.dart.lib.js:34:35)
    at main.next (<anonymous>)
    at runBody (http://localhost:59823/dart_sdk.js:39260:34)
    at Object._async [as async] (http://localhost:59823/dart_sdk.js:39291:7)
    at main$ (http://localhost:59823/web_entrypoint.dart.lib.js:33:18)
    at http://localhost:59823/main_module.bootstrap.js:19:10
    at Array.forEach (<anonymous>)
    at window.$dartRunMain (http://localhost:59823/main_module.bootstrap.js:18:32)
    at <anonymous>:1:8
    at Object.runMain (http://localhost:59823/dwds/src/injected/client.js:8824:21)
    at http://localhost:59823/dwds/src/injected/client.js:22712:19
    at _wrapJsFunctionForAsync_closure.$protected (http://localhost:59823/dwds/src/injected/client.js:3850:15)
    at _wrapJsFunctionForAsync_closure.call$2 (http://localhost:59823/dwds/src/injected/client.js:11062:12)
    at Object._asyncStartSync (http://localhost:59823/dwds/src/injected/client.js:3814:20)
    at main__closure1.$call$body$main__closure (http://localhost:59823/dwds/src/injected/client.js:22724:16)
    at main__closure1.call$1 (http://localhost:59823/dwds/src/injected/client.js:22651:19)
    at StaticClosure._rootRunUnary [as call$2$5] (http://localhost:59823/dwds/src/injected/client.js:4184:16)
    at _CustomZone.runUnary$2$2 (http://localhost:59823/dwds/src/injected/client.js:12435:39)
    at _CustomZone.runUnaryGuarded$1$2 (http://localhost:59823/dwds/src/injected/client.js:12382:14)
    at _ControllerSubscription._sendData$1 (http://localhost:59823/dwds/src/injected/client.js:11958:19)
    at _DelayedData.perform$1 (http://localhost:59823/dwds/src/injected/client.js:12147:59)
    at _PendingEvents_schedule_closure.call$0 (http://localhost:59823/dwds/src/injected/client.js:12196:14)
    at Object._microtaskLoop (http://localhost:59823/dwds/src/injected/client.js:4022:24)
    at StaticClosure._startMicrotaskLoop (http://localhost:59823/dwds/src/injected/client.js:4028:11)
    at _AsyncRun__initializeScheduleImmediate_internalCallback.call$1 (http://localhost:59823/dwds/src/injected/client.js:10931:9)
    at invokeClosure (http://localhost:59823/dwds/src/injected/client.js:1258:26)
    at MutationObserver.<anonymous> (http://localhost:59823/dwds/src/injected/client.js:1277:18)``` 

【问题讨论】:

【参考方案1】:

您需要按照guide 进行网络安装,然后才能使用它。

将以下内容添加到您的index.htmlbody

<script src="https://www.gstatic.com/firebasejs/8.4.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.4.1/firebase-analytics.js"></script>

<script>
      var firebaseConfig = 
        apiKey: "...",
        authDomain: "[YOUR_PROJECT].firebaseapp.com",
        databaseURL: "https://[YOUR_PROJECT].firebaseio.com",
        projectId: "[YOUR_PROJECT]",
        storageBucket: "[YOUR_PROJECT].appspot.com",
        messagingSenderId: "...",
        appId: "1:...:web:...",
        measurementId: "G-...",
      ;

      // Initialize Firebase
      firebase.initializeApp(firebaseConfig);
</script>

您需要为您使用的每个 SDK 添加脚本。以上只是展示了分析的基础知识。

【讨论】:

以上是关于Flutter Web 错误 TypeError: dart.global.firebase.analytics is not a function的主要内容,如果未能解决你的问题,请参考以下文章

TypeError: Cannot read properties of undefined (reading 'app') (Flutter web app)

TypeError: dart.global.firebase.storage 不是 Flutter web 上的函数

Flutter web Firebase 分析类型错误:dart.global.firebase.analytics 不是函数

将firebase添加到flutter web时出现问题,TypeError:无法读取未定义的属性(读取“应用程序”)

Flutter Web和Firebase身份验证TypeError:无法读取未定义的属性'app'

Flutter 错误:_TypeError(类型“Null”不是“FutureOr<Database>”类型的子类型)