Flutter, FirebaseError: Firebase: No Firebase App '[DEFAULT]' has been created - 调用 Firebase App.ini

Posted

技术标签:

【中文标题】Flutter, FirebaseError: Firebase: No Firebase App \'[DEFAULT]\' has been created - 调用 Firebase App.initializeApp() (app/no-app)【英文标题】:Flutter, FirebaseError: Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() (app/no-app)Flutter, FirebaseError: Firebase: No Firebase App '[DEFAULT]' has been created - 调用 Firebase App.initializeApp() (app/no-app) 【发布时间】:2021-09-09 03:41:15 【问题描述】:

我正在尝试在 Web 上运行我的移动 Flutter 应用程序,我想我完成了我需要做的所有配置,但我收到错误:“FirebaseError: Firebase: No Firebase App '[DEFAULT]' has been created - 调用 Firebase App.initializeApp() (app/no-app)。”有谁知道我该怎么做才能解决它?我正在 main 中初始化 firebase,我找不到我还能做什么。

这是我的 main.dart:

import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:untitled1/quiz_app/sign_in.dart';

void main() async
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MaterialApp(

    //initialRoute: '/SignIn',

    debugShowCheckedModeBanner: false,
    theme: ThemeData(
        primarySwatch: Colors.indigo
    ),
    routes: 
      '/SignIn' :(context) => SignInPage(),

    ,
    home: Scaffold(
        resizeToAvoidBottomInset: false,
      body: SignInPage()
    ),
  ));

这里是 index.html

<!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="ahmett">
  <link rel="apple-touch-icon" href="icons/Icon-192.png">

  <title>ahmett</title>
  <link rel="manifest" href="manifest.json">
</head>
<body>
<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/8.6.8/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/8.6.8/firebase-analytics.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.6.8/firebase-storage.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.6.8/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.6.8/firebase-firestore.js"></script

<script>
  // Your web app's Firebase configuration
  // For Firebase JS SDK v7.20.0 and later, measurementId is optional
  var firebaseConfig = 
    apiKey: "cencored",
    authDomain: "cencored",
    projectId: "cencored",
    storageBucket: "cencored",
    messagingSenderId: "cencored",
    appId: "cencored",
    measurementId: "cencored"
  ;
  // Initialize Firebase
  firebase.initializeApp(firebaseConfig);
  firebase.analytics();
</script>
</script><script src="main.dart.js" type="application/javascript"></script>
</body>

</html>

【问题讨论】:

***.com/questions/65353335/… 您可以使用此表单,但将 MyApp() 放在 runApp() 中,然后将您的应用程序编写为函数小部件 MyApp 我将 main.dart 更改为那个,但仍然出现同样的错误 问题与我的 index.html 文件有关,但我也在 main.dart 中使用了您的格式,所以谢谢! 没问题,很高兴看到您解决了问题。快乐编码 Ahmet Hakan kardesim 【参考方案1】:

对于 Flutter Web,Firebase 插件有一个新设置。 首先,按照这里提到的所有步骤https://firebase.flutter.dev/docs/installation/web

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>example</title>
</head>
<body>
  <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-messaging.js"></script>
  <script>
    // Your web app's Firebase configuration
    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>
  <script src="main.dart.js" type="application/javascript"></script>
  <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 () 
        //register firebase-messaging service worker
        navigator.serviceWorker.register("/firebase-messaging-sw.js");
        // 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>
</body>
</html>

在以下位置查看此设置 https://github.com/FirebaseExtended/flutterfire/tree/master/packages/firebase_messaging/firebase_messaging/example/web

【讨论】:

以上是关于Flutter, FirebaseError: Firebase: No Firebase App '[DEFAULT]' has been created - 调用 Firebase App.ini的主要内容,如果未能解决你的问题,请参考以下文章

FirebaseError:权限缺失或不足。 -类星体和火力基地

FirebaseError:无法获取文档,因为客户端处于脱机状态

FirebaseError:firebase.initializeApp 中未提供“projectId”

请求过多 FirebaseError

FirebaseError:[code=permission-denied]:权限缺失或不足

FirebaseError:预期类型“Hc”,但它是:自定义 Yc 对象]