dimanche 21 novembre 2021

No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() on WEB

I tried everything people wrote on this problem and nothing worked. I have two apps, one was designed originally for web and one for mobile. I compared their codes in the web related parts - it's the same. the web one works, the mobile doesn't. I went through all the discussion here about it and couldn't find the problem.

the only clue that I have is that when I run the app on web, it doesn't even start to run my main.dart file. Or maybe it is, I put a breakpoint my main function and it never reaches it. it stops on a file named web_entrypoint.dart. The file has a "Future main()" function that starts and then throws the error.

Thank you!

the error:

    at StaticClosure._rootRunUnary (http://localhost:50521/dwds/src/injected/client.js:4830:16)
    at _CustomZone.runUnary$2$2 (http://localhost:50521/dwds/src/injected/client.js:12880:39)
    at _CustomZone.runUnaryGuarded$1$2 (http://localhost:50521/dwds/src/injected/client.js:12827:14)
    at _CustomZone_bindUnaryCallbackGuarded_closure.call$1 (http://localhost:50521/dwds/src/injected/client.js:13017:25)
    at invokeClosure (http://localhost:50521/dwds/src/injected/client.js:1524:26)
    at WebSocket.<anonymous> (http://localhost:50521/dwds/src/injected/client.js:1543:18)

my main.dart file:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  //await initializeFlutterFire();

  SystemChrome.setPreferredOrientations(
      [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
  Wakelock.enable();
  runApp(const MyApp());
}

web_entrypoint.dart

// @dart=2.12
// Flutter web bootstrap script for package:app/main.dart.

import 'dart:ui' as ui;
import 'dart:async';

import 'package:app/main.dart' as entrypoint;
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:app/generated_plugin_registrant.dart';

typedef _UnaryFunction = dynamic Function(List<String> args);
typedef _NullaryFunction = dynamic Function();
Future<void> main() async {
  registerPlugins(webPluginRegistrar);
  await ui.webOnlyInitializePlatform();
  if (entrypoint.main is _UnaryFunction) {
    return (entrypoint.main as _UnaryFunction)(<String>[]);
  }
  return (entrypoint.main as _NullaryFunction)();
}

pubspec.yaml dependencies:

dependencies:
  flutter:
    sdk: flutter
  google_fonts: ^2.1.0
  provider: ^6.0.1
  path_provider: ^2.0.5
  path: ^1.8.0
  http: ^0.13.4
  device_info_plus: ^3.0.1
  flutter_keyboard_visibility: ^5.0.3
  image_picker: ^0.8.4+2
  datetime_picker_formfield: ^2.0.0
  firebase_core: ^1.7.0
  cloud_firestore: ^2.5.3
  firebase_storage: ^10.0.6
  firebase_analytics: ^8.3.3
  faker: ^2.0.0
  auto_size_text: ^3.0.0-nullsafety.0
  just_audio: ^0.9.13 
  firebase_auth: ^3.1.3
  device_info: ^2.0.2
  wakelock: ^0.5.6
  transparent_image: ^2.0.0
  flutter_countdown_timer: ^4.1.0
  audioplayers: ^0.20.1
  firebase_app_check: ^0.0.2+3
  animated_widgets: ^1.1.0
  restart_app: ^1.1.0
  connectivity_plus: ^2.0.2
  translator: ^0.1.7
  package_info_plus: ^1.3.0
  gsheets: ^0.3.2
  flutter_speed_dial: ^4.6.6
  firebase_core_web: ^1.2.0

index.html file (with app details replaced):

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

  <title>apptitle</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 src="https://www.gstatic.com/firebasejs/8.6.1/firebase-app.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.5.0/firebase-app.js";
        import { getAnalytics } from "https://www.gstatic.com/firebasejs/9.5.0/firebase-analytics.js"; -->
        <script>
        // 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
        var firebaseConfig = {
          apiKey: "----------",
          authDomain: "----------,
          databaseURL: "----------",
          projectId: "z----------",
          storageBucket: "----------",
          messagingSenderId: "----------",
          appId: "----------",
          measurementId: "----------"
        };
        
      
        // Initialize Firebase
        var app = initializeApp(firebaseConfig);
       // var analytics = getAnalytics(app);
      </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 () {
        // 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>

flutter doctor:

[√] Flutter (Channel dev, 2.6.0-11.0.pre, on Microsoft Windows [Version 10.0.19043.1348], locale en-IL)
    • Flutter version 2.6.0-11.0.pre at C:\Users\---\Documents\developer\sdks\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 4b330ddbed (9 weeks ago), 2021-09-16 17:29:58 -0700
    • Engine revision 5b81c6d615
    • Dart version 2.15.0 (build 2.15.0-116.0.dev)

[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at C:\Users\---\AppData\Local\Android\sdk
    • Platform android-31, build-tools 31.0.0
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.11.5)
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
    • Visual Studio Community 2019 version 16.11.31729.503
    • Windows 10 SDK version 10.0.19041.0

[√] Android Studio (version 2020.3)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)

[√] VS Code (version 1.62.3)
    • VS Code at C:\Users\---\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.28.0

[√] Connected device (4 available)
    • Windows (desktop)       • windows • windows-x64     • Microsoft Windows [Version 10.0.19043.1348]
    • Windows (UWP) (desktop) • winuwp  • windows-uwp-x64 • 
    • Chrome (web)            • chrome  • web-javascript  • Google Chrome 96.0.4664.45
    • Edge (web)              • edge    • web-javascript  • Microsoft Edge 95.0.1020.53

• No issues found!



Aucun commentaire:

Enregistrer un commentaire