“未捕获的ReferenceError:未定义firebase”如何在Chrome中定义Firebase并超越空白屏幕?

问题描述

我正在将Flutter网站集成到现有应用程序中。现有应用程序可以在iOS和Android上正常运行。该应用程序利用FlutterFire。使用以下内容,当运行Chrome设备时,出现空白屏幕。

pubspec.yaml

environment: 


sdk: '>=2.6.0 <3.0.0'

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^0.1.2
  
  firebase: ^7.3.0
  firebase_core: ^0.5.0
  cloud_firestore: ^0.14.0+2
  firebase_analytics: ^6.0.0
  firebase_auth: ^0.18.0+1

index.html

<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/7.20.-/firebase-app.js"></script>

<!-- TODO: Add SDKs for Firebase products that you want to use
     https://firebase.google.com/docs/web/setup#available-libraries -->
<script src="https://www.gstatic.com/firebasejs/7.20.-/firebase-analytics.js"></script>


<script src="https://www.gstatic.com/firebasejs/7.20.-/firebase-firestore.js"></script>


<script src="https://www.gstatic.com/firebasejs/7.20.-/firebase-auth.js"></script>

<script>
  // Your web app's Firebase configuration
  // For Firebase JS SDK v7.20.0 and later,measurementId is optional
  var firebaseConfig = {
    apiKey: "myKeyHere",authDomain: "myDomainHere",databaseURL: "myURLHere",projectId: "myProjIDHere",storageBucket: "myBucketHere",messagingSenderId: "myMessageID",appId: "myAppID",measurementId: "myMeasID"
  };
  // Initialize Firebase
  firebase.initializeApp(firebaseConfig);
  firebase.analytics();
</script>

<!-- This script installs service_worker.js to provide PWA functionality to
       application. For more information,see:
       https://developers.google.com/web/fundamentals/primers/service-workers -->
  <script>
    if ('serviceWorker' in navigator) {
      window.addEventListener('load',function () {
        navigator.serviceWorker.register('flutter_service_worker.js');
      });
    }
  </script>

  
  <script defer src="main.dart.js" type="application/javascript"></script>

</body>

这会在VSCode控制台中导致以下错误:

TypeError: Cannot read property 'app' of undefined
    at Object.app$ [as app] (http://localhost:58913/packages/firebase/src/top_level.dart.lib.js:78:89)
    at new cloud_firestore_web.FirebaseFirestoreWeb.new (http://localhost:58913/packages/cloud_firestore_web/src/write_batch_web.dart.lib.js:755:57)
    at Function.registerWith (http://localhost:58913/packages/cloud_firestore_web/src/write_batch_web.dart.lib.js:671:73)
    at Object.registerPlugins ...

以及Chrome控制台中的以下错误:

Uncaught ReferenceError: firebase is not defined
    at localhost/:50
:58913/packages/firebase/src/top_level.dart.lib.js:78 Uncaught (in promise) TypeError: Cannot read property 'app' of undefined
    at Object.app$ [as app] (:58913/packages/firebase/src/top_level.dart.lib.js:78)
    at new cloud_firestore_web.FirebaseFirestoreWeb.new (:58913/packages/cloud_firestore_web/src/write_batch_web.dart.lib.js:755)
    at Function.registerWith (:58913/packages/cloud_firestore_web/src/write_batch_web.dart.lib.js:671)
    at Object.registerPlugins (:58913/packages/carlinehound/generated_plugin_registrant.dart.lib.js:23)
    at main (:58913/web_entrypoint.dart.lib.js:29)
    at main.next (<anonymous>)

我已经使用了Future Builder和Streambuilder FlutterFire应用程序初始化方法。我也只是在main()中初始化了。所有这些都会导致相同的错误。

问题显然是未定义firebase。我的问题是如何定义Firebase?

解决方法

index.html中添加JS SDK时,您没有指定完整的版本号

7.20.-不是真实版本,它只是一个占位符,供您指定要使用的版本。

您可以通过在浏览器中转到src进行检查。您会看到您当前拥有的src个将不存在。我在下面显示的内容将显示一堆JS。

如果要使用最新版本,则为7.20.0。

<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/7.20.0/firebase-app.js"></script>

<!-- TODO: Add SDKs for Firebase products that you want to use
     https://firebase.google.com/docs/web/setup#available-libraries -->
<script src="https://www.gstatic.com/firebasejs/7.20.0/firebase-analytics.js"></script>


<script src="https://www.gstatic.com/firebasejs/7.20.0/firebase-firestore.js"></script>


<script src="https://www.gstatic.com/firebasejs/7.20.0/firebase-auth.js"></script>

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...