FirebaseError:Firebase:未创建 Firebase 应用程序“[DEFAULT]” - 首先调用initializeApp()(应用程序/无应用程序)。颤抖中

问题描述 投票:0回答:1

我正在尝试在 chrome 网络版上运行我的 flutter 应用程序。但每次我运行我的颤振代码时。遇到以下错误。

enter image description here

  • 我已将
    google-services.json
    添加到
    <Project>/android/app/
  • 我的应用程序在我的物理设备和应用程序版本的模拟器上运行没有任何错误

我的问题仅在于这个颤振代码的网络。在其他平台(Android、Windows 等)中,代码运行良好

代码在

main.dart

import 'package:attandance_seventh/firebase_options.dart';
import 'package:attandance_seventh/globals/bindings.dart';
import 'package:attandance_seventh/services/remote/firestore/notification.dart';
import 'package:attandance_seventh/views/launch_screen/launch_screen.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
// ignore: depend_on_referenced_packages
import 'package:firebase_core/firebase_core.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await ScreenUtil.ensureScreenSize();
  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
  await initNotification();
  await GetStorage.init();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      minTextAdapt: true,
      splitScreenMode: true,
      builder: (context, child) {
        return GetMaterialApp(
          title: 'Flutter Demo',
          initialBinding: GlabalBindings(),
          debugShowCheckedModeBanner: false,
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          // home: const SplashScreen(),
          home: Scaffold(
            body: Center(
              child: Text('Hello kids'),
            ),
          ),
        );
      },
    );
  }
}

代码在

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

  <!-- Favicon -->
  <link rel="icon" type="image/png" href="favicon.png"/>

  <title>attandance_seventh</title>
  <link rel="manifest" href="manifest.json">
 
  <!-- <script src="main.dart.js" type="application/javascript"></script> -->
  <script>
    // The value below is injected by flutter build, do not touch.
    const serviceWorkerVersion = null;
  </script>
  <!-- This script adds the flutter initialization JS code -->
  <script src="flutter.js" defer></script>
</head>
<body>

  <script>
    window.addEventListener('load', function(ev) {
      // Download main.dart.js
      _flutter.loader.loadEntrypoint({
        serviceWorker: {
          serviceWorkerVersion: serviceWorkerVersion,
        },
        onEntrypointLoaded: function(engineInitializer) {
          engineInitializer.initializeEngine().then(function(appRunner) {
            appRunner.runApp();
          });
        }
      });
    });
  </script>
 <script type="module">
  // Import the functions you need from the SDKs you need
  import { initializeApp } from "https://www.gstatic.com/firebasejs/10.12.2/firebase-app.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
  const firebaseConfig = {
    apiKey: "<-------- My API Key---------->",
    authDomain: "<-------- Auth Domain---------->",
    projectId: "<-------- Project ID---------->",
    storageBucket: "<-------- Storage Bucket---------->",
    messagingSenderId: "<-------- Messaging SenderId---------->",
    appId: "<-------- App Id---------->"
  };

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

请帮助我!!!

android ios flutter firebase
1个回答
0
投票

您可以指定

Options
值。

options: const FirebaseOptions(
            apiKey: "AIzaSyD3ZbXXXXXXXXXXXXXXX-XXXX",
            authDomain: "XXXXXXXXXX.firebaseapp.com",
            projectId: "XXXXXXX",
            storageBucket: "XXXXXXXX.appspot.com",
            messagingSenderId: "XXXXXXXXX",
            appId: "1:XXXXXX:web:XXXXXXXX",
            measurementId: "G-XXXXXXX")
© www.soinside.com 2019 - 2024. All rights reserved.