首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何设置web firebase web

如何设置web firebase web
EN

Stack Overflow用户
提问于 2021-11-01 11:53:57
回答 1查看 97关注 0票数 0

我试着为设置火基,我遵循了所有的程序。但在运行应用程序后,它变得白色,并显示以下错误

代码语言:javascript
运行
复制
 ‘TypeError: Cannot read property ‘app’ of undefined’ In Flutter Web

然后我去谷歌搜索解决方案,并按照他们要求的方式做了所有的事情,但我最终得到了null安全错误。在那之后,我删除了null安全性,这就是我现在得到的错误

代码语言:javascript
运行
复制
Running with unsound null safety
For more information see https://dart.dev/null-safety/unsound-null-safety
Debug service listening on ws://127.0.0.1:57090/YI8fSoqgm_w=/ws
FirebaseError: Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() (app/no-app).
    at Object.u [as app] (https://www.gstatic.com/firebasejs/8.6.1/firebase-app.js:1:18836)
    at Object.app$ [as app] (http://localhost:57026/packages/firebase_core_web/src/interop/core.dart.lib.js:31:101)
    at new cloud_firestore_web.FirebaseFirestoreWeb.new (http://localhost:57026/packages/cloud_firestore_web/src/write_batch_web.dart.lib.js:934:64)
    at Function.registerWith (http://localhost:57026/packages/cloud_firestore_web/src/write_batch_web.dart.lib.js:834:73)
    at Object.registerPlugins (http://localhost:57026/packages/tembea/generated_plugin_registrant.dart.lib.js:18:46)
    at main (http://localhost:57026/web_entrypoint.dart.lib.js:31:35)
    at main.next (<anonymous>)
    at runBody (http://localhost:57026/dart_sdk.js:40666:34)
    at Object._async [as async] (http://localhost:57026/dart_sdk.js:40697:7)
    at main$ (http://localhost:57026/web_entrypoint.dart.lib.js:30:18)
    at http://localhost:57026/main_module.bootstrap.js:19:10
    at Array.forEach (<anonymous>)
    at window.$dartRunMain (http://localhost:57026/main_module.bootstrap.js:18:32)
    at <anonymous>:1:8
    at Object.runMain (http://localhost:57026/dwds/src/injected/client.js:8452:21)
    at http://localhost:57026/dwds/src/injected/client.js:24130:19
    at _wrapJsFunctionForAsync_closure.$protected (http://localhost:57026/dwds/src/injected/client.js:4461:15)
    at _wrapJsFunctionForAsync_closure.call$2 (http://localhost:57026/dwds/src/injected/client.js:11511:12)
    at Object._asyncStartSync (http://localhost:57026/dwds/src/injected/client.js:4425:20)
    at main__closure3.$call$body$main__closure (http://localhost:57026/dwds/src/injected/client.js:24142:16)
    at main__closure3.call$1 (http://localhost:57026/dwds/src/injected/client.js:24069:19)
    at StaticClosure._rootRunUnary (http://localhost:57026/dwds/src/injected/client.js:4823:18)
    at _CustomZone.runUnary$2$2 (http://localhost:57026/dwds/src/injected/client.js:12879:39)
    at _CustomZone.runUnaryGuarded$1$2 (http://localhost:57026/dwds/src/injected/client.js:12826:14)
    at _ForwardingStreamSubscription._sendData$1 (http://localhost:57026/dwds/src/injected/client.js:12416:19)
    at _ForwardingStreamSubscription._add$1 (http://localhost:57026/dwds/src/injected/client.js:12362:15)
    at _ForwardingStreamSubscription._add$1 (http://localhost:57026/dwds/src/injected/client.js:12695:12)
    at _MapStream._handleData$2 (http://localhost:57026/dwds/src/injected/client.js:12756:12)
    at _ForwardingStreamSubscription._handleData$1 (http://localhost:57026/dwds/src/injected/client.js:12721:20)
    at Object.eval (eval at Closure_forwardCallTo (http://localhost:57026/dwds/src/injected/client.js:1701:14), <anonymous>:3:44)
    at StaticClosure._rootRunUnary (http://localhost:57026/dwds/src/injected/client.js:4823:18)
    at _CustomZone.runUnary$2$2 (http://localhost:57026/dwds/src/injected/client.js:12879:39)
    at _CustomZone.runUnaryGuarded$1$2 (http://localhost:57026/dwds/src/injected/client.js:12826:14)
    at _ControllerSubscription._sendData$1 (http://localhost:57026/dwds/src/injected/client.js:12416:19)
    at _ControllerSubscription._add$1 (http://localhost:57026/dwds/src/injected/client.js:12362:15)
    at _SyncStreamController._sendData$1 (http://localhost:57026/dwds/src/injected/client.js:12201:32)
    at _SyncStreamController.add$1 (http://localhost:57026/dwds/src/injected/client.js:12082:15)
    at Object.eval (eval at Closure_forwardInterceptedCallTo (http://localhost:57026/dwds/src/injected/client.js:1781:14), <anonymous>:3:45)
    at StaticClosure._rootRunUnary (http://localhost:57026/dwds/src/injected/client.js:4823:18)
    at _CustomZone.runUnary$2$2 (http://localhost:57026/dwds/src/injected/client.js:12879:39)
    at _CustomZone.runUnaryGuarded$1$2 (http://localhost:57026/dwds/src/injected/client.js:12826:14)
    at _ControllerSubscription._sendData$1 (http://localhost:57026/dwds/src/injected/client.js:12416:19)
    at _ControllerSubscription._add$1 (http://localhost:57026/dwds/src/injected/client.js:12362:15)
    at _SyncStreamController._sendData$1 (http://localhost:57026/dwds/src/injected/client.js:12201:32)
    at _SyncStreamController.add$1 (http://localhost:57026/dwds/src/injected/client.js:12082:15)
    at _GuaranteeSink.add$1 (http://localhost:57026/dwds/src/injected/client.js:23581:25)
    at HtmlWebSocketChannel_closure1.call$1 (http://localhost:57026/dwds/src/injected/client.js:23874:60)
    at _EventStreamSubscription_closure.call$1 (http://localhost:57026/dwds/src/injected/client.js:18042:26)
    at StaticClosure._rootRunUnary (http://localhost:57026/dwds/src/injected/client.js:4829:16)
    at _CustomZone.runUnary$2$2 (http://localhost:57026/dwds/src/injected/client.js:12879:39)
    at _CustomZone.runUnaryGuarded$1$2 (http://localhost:57026/dwds/src/injected/client.js:12826:14)
    at _CustomZone_bindUnaryCallbackGuarded_closure.call$1 (http://localhost:57026/dwds/src/injected/client.js:13016:25)
    at invokeClosure (http://localhost:57026/dwds/src/injected/client.js:1524:26)
    at WebSocket.<anonymous> (http://localhost:57026/dwds/src/injected/client.js:1543:18)

下面是我的main函数

代码语言:javascript
运行
复制
void main() async{
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(const MyApp());

和pubspec

环境: sdk:">=2.12.0 <3.0.0“

代码语言:javascript
运行
复制
dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^1.0.2
  flutter_signin_button: ^2.0.0
  firebase_auth: ^3.1.4
  firebase_core: ^1.8.0
  cloud_firestore: ^2.5.4
  provider: ^6.0.1
  firebase_analytics: ^7.0.1

最后是index.html

代码语言:javascript
运行
复制
<!DOCTYPE html>
<html>
<head>
  <!--
    If you are serving your web app in a path other than the root, change the
    href value below to reflect the base path you are serving from.

    The path provided below has to start and end with a slash "/" in order for
    it to work correctly.

    For more details:
    * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base

    This is a placeholder for base href that will be replaced by the value of
    the `--base-href` argument provided to `flutter build`.
  -->
  <base href="$FLUTTER_BASE_HREF">

  <meta charset="UTF-8">
  <meta content="IE=Edge" http-equiv="X-UA-Compatible">
  <meta name="description" content="A new Flutter project.">

  <!-- iOS meta tags & icons -->
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="apple-mobile-web-app-title" content="tembea">
  <link rel="apple-touch-icon" href="icons/Icon-192.png">

  <title>tembea</title>
  <link rel="manifest" href="manifest.json">
</head>
<body>
  <!-- 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>
    var serviceWorkerVersion = null;
    var scriptLoaded = false;
    function loadMainDartJs() {
      if (scriptLoaded) {
        return;
      }
      scriptLoaded = true;
      var scriptTag = document.createElement('script');
      scriptTag.src = 'main.dart.js';
      scriptTag.type = 'application/javascript';
      document.body.append(scriptTag);
    }

    if ('serviceWorker' in navigator) {
      // Service workers are supported. Use them.
      window.addEventListener('load', function () {
        // Wait for registration to finish before dropping the <script> tag.
        // Otherwise, the browser will load the script multiple times,
        // potentially different versions.
        var serviceWorkerUrl = 'flutter_service_worker.js?v=' + serviceWorkerVersion;
        navigator.serviceWorker.register(serviceWorkerUrl)
          .then((reg) => {
            function waitForActivation(serviceWorker) {
              serviceWorker.addEventListener('statechange', () => {
                if (serviceWorker.state == 'activated') {
                  console.log('Installed new service worker.');
                  loadMainDartJs();
                }
              });
            }
            if (!reg.active && (reg.installing || reg.waiting)) {
              // No active web worker and we have installed or are installing
              // one for the first time. Simply wait for it to activate.
              waitForActivation(reg.installing || reg.waiting);
            } else if (!reg.active.scriptURL.endsWith(serviceWorkerVersion)) {
              // When the app updates the serviceWorkerVersion changes, so we
              // need to ask the service worker to update.
              console.log('New service worker available.');
              reg.update();
              waitForActivation(reg.installing);
            } else {
              // Existing service worker is still good.
              console.log('Loading app from service worker.');
              loadMainDartJs();
            }
          });

        // If service worker doesn't succeed in a reasonable amount of time,
        // fallback to plaint <script> tag.
        setTimeout(() => {
          if (!scriptLoaded) {
            console.warn(
              'Failed to load app from service worker. Falling back to plain <script> tag.',
            );
            loadMainDartJs();
          }
        }, 4000);
      });
    } else {
      // Service workers not supported. Just drop the <script> tag.
      loadMainDartJs();
    }
  </script>
  <script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-app.js"></script>
  <script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-analytics.js"></script>
  <script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-auth.js"></script>
  <script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-firestore.js"></script>

  <script type="module">
  // Import the functions you need from the SDKs you need
  import { initializeApp } from "https://www.gstatic.com/firebasejs/9.2.0/firebase-app.js";
  import { getAnalytics } from "https://www.gstatic.com/firebasejs/9.2.0/firebase-analytics.js";
  // TODO: Add SDKs for Firebase products that you want to use
  // https://firebase.google.com/docs/web/setup#available-libraries

  // Your web app's Firebase configuration
  // For Firebase JS SDK v7.20.0 and later, measurementId is optional
  const firebaseConfig = {
    apiKey: "AIzaSyAQYkB8TMTJKMmdzmjVQwsyiQrfMaXqOao",
    authDomain: "tembea-4d3c6.firebaseapp.com",
    projectId: "tembea-4d3c6",
    storageBucket: "tembea-4d3c6.appspot.com",
    messagingSenderId: "909705170922",
    appId: "1:909705170922:web:d034fefe33c9d0d2026c93",
    measurementId: "G-11NNW6J10N"
  };

  // Initialize Firebase
  const app = initializeApp(firebaseConfig);
  const analytics = getAnalytics(app);
</script>
</body>
</html>

我不知道我做错了什么。任何帮助都将受到欢迎。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-01 12:24:09

对于Firebase,您有两个单独的导入,这可能会导致问题。删除您的<script type="module">... import...</script>,并将其替换为不同的初始化样式,如下所示:

代码语言:javascript
运行
复制
<script>
  const firebaseConfig = {
    apiKey: "AIzaSyAQYkB8TMTJKMmdzmjVQwsyiQrfMaXqOao",
    authDomain: "tembea-4d3c6.firebaseapp.com",
    projectId: "tembea-4d3c6",
    storageBucket: "tembea-4d3c6.appspot.com",
    messagingSenderId: "909705170922",
    appId: "1:909705170922:web:d034fefe33c9d0d2026c93",
    measurementId: "G-11NNW6J10N"
  };

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

这是当前推荐的方式,可以在Flutter Firebase docs中找到。

由于您是在main函数中初始化Firebase,因此您可能还希望将应用程序启动脚本移到Firebase初始化脚本下。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69796555

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档