Flutter: Uncaught ReferenceError: firebase is not defined
Asked Answered
R

1

7

I'm using Firebase services in Flutter web app and having the issue with loading firebase script.

When I'm using flutter run -d chrome I see blank page with error and after reload page is loaded fine :-/.

When I do flutter build web - the page is never loaded, reload doesn't help.

Underlying error: Uncaught ReferenceError: firebase is not defined

Here's what I see on first launch when I do flutter run -d chrome:

Uncaught ReferenceError: firebase is not defined
    at (index):26
app.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
require.js:143 Uncaught Error: Script error for "@firebase/app", needed by: dart_sdk
http://requirejs.org/docs/errors.html#scripterror
    at makeError (require.js:168)
    at HTMLScriptElement.onScriptError (require.js:1738)
:57663/favicon.ico:1 Failed to load resource: the server responded with a status of 404 (Not Found)

When I do flutter build web - only this error:

Uncaught ReferenceError: firebase is not defined
    at Object.aoq (top_level.dart:80)
    at Object.auD (cloud_firestore_web.dart:33)
    at cloud_firestore_web.dart:26
    at aom.a (async_patch.dart:315)
    at aom.$2 (async_patch.dart:340)
    at Object.G (async_patch.dart:245)
    at Object.UR (main.dart:8)
    at js_helper.dart:3246
    at js_helper.dart:3246
    at dartProgram (js_helper.dart:3246)

My index.html file:

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>new_flowers</title>
</head>
<body>
  <script src="main.dart.js" type="application/javascript"></script>
  <script src="https://www.gstatic.com/firebasejs/7.15.0/firebase-app.js"></script>
  <script src ="https://www.gstatic.com/firebasejs/7.15.0/firebase-firestore.js"></script>
  <script src="https://www.gstatic.com/firebasejs/7.15.0/firebase-auth.js"></script>
  <script>
  // Your web app's Firebase configuration
  var firebaseConfig = {
    apiKey: "...",
    authDomain: "...",
    databaseURL: "...",
    projectId: "...",
    storageBucket: "...",
    messagingSenderId: "...",
    appId: "...",
    measurementId: "..."
  };

  // Initialize Firebase
  firebase.initializeApp(firebaseConfig);
  </script>
</body>
</html>
Regelation answered 18/6, 2020 at 11:11 Comment(0)
T
15

Your index.html is a little bit messed up. Try defining your scripts like this :

  1. Firebase app + used CDNs
  2. Firebase app config
  3. Service worker
  4. Main.dart.js

EDIT: Code won't format correctly, here's the link to html code: `

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

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

  <title>BEvent</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/7.15.1/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.15.1/firebase-auth.js"></script>
  <script src="https://www.gstatic.com/firebasejs/7.15.1/firebase-firestore.js"></script>
  <script src="https://www.gstatic.com/firebasejs/7.15.2/firebase-storage.js"></script>
  <script src="https://www.gstatic.com/firebasejs/7.15.1/firebase-analytics.js"></script>

  <script>
    // Your web app's Firebase configuration
    var firebaseConfig = {
      ...
    };
    // 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 src="main.dart.js" type="application/javascript"></script>
</body>

</html>`
Thorrlow answered 24/6, 2020 at 4:35 Comment(5)
your code works like magic. What service worker is for and where did you get info to put it in?Antiquary
When you create a flutter project it gets added automatically.Thorrlow
Is there any way in which you could add the firebase configuration in dart and not HTML?Colwin
I don't think so because you must load firebase sdks before dart & flutterThorrlow
You're a hero, followed these exactly. My webappw as running lcoally, but after publishing onle, it was only a white screen with undefined firebase. This orientation of JS entries, made it work.Henbit

© 2022 - 2024 — McMap. All rights reserved.