Sebastien FERRAND
Sebastien FERRAND

Reputation: 2150

Flutter existing project to WebApp + Firebase (Firestore + Hosting)

I have an existing Flutter app (runs correctly on Android/IOS) using Firestore.

GOAL:

  1. I'd like to first have it support web
  2. I'd like to host it on Firebase hosting

Results so far:

  1. When running in local, blank page
  2. When deploying to firebase, doesn't even seem to run index.html

What I tried

  1. According to this tutorial :

    a) Added a Web App on my firebase project

    b) npm install -g firebase-tools

    c) firebase login

    d) firebase init

    ==> I select Firestore and Hosting

    ==> Use existing project, select my project

    ==> use all defaut setup (except build/web folder for public directory)

    e) flutter channel dev flutter upgrade flutter config --enable-web

    f) flutter create .

    g) flutter build web

    h) I then copy all the content from build/web to /web folder, add the CDN from firebase at the bottom of the body, then call Firebase JS SDKs the following way :

ERRORS

I get this error when running locally on chrome:

(index):53 Uncaught ReferenceError: firebase is not defined

As for using firebase deploy, the page says I successfully set up hosting but does not show index.html

For reference :

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

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

    <title>kidz_happy_pawn</title>
    <link rel="manifest" href="manifest.json">
    <script src='https://cdn.firebase.com/js/client/2.2.1/firebase.js'></script>
    <script src='https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js'></script>
    <link rel='stylesheet' type='text/css' href='/resources/tutorial/css/example.css'>
</head>
<body>

<div id="message">
    <h2>Welcome</h2>
    <h1>Firebase Hosting Setup Complete</h1>
    <p>You're seeing this because you've successfully setup Firebase Hosting. Now it's time to go
        build something extraordinary!</p>
</div>

<script src="https://www.gstatic.com/firebasejs/7.5.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.5.0/firebase-firestore.js"></script>
<script>
  var firebaseConfig = {
    apiKey: "xxx-from-firebase",
    authDomain: "xxx-from-firebase",
    databaseURL: "xxx-from-firebase",
    projectId: "xxx-from-firebase",
    storageBucket: "xxx-from-firebase",
    messagingSenderId: "xxx-from-firebase",
    appId: "xxx-from-firebase",
    measurementId: "xxx-from-firebase"
  };
  firebase.initializeApp(firebaseConfig);
  firebase.analytics();

</script>

<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>

Upvotes: 0

Views: 879

Answers (1)

griffins
griffins

Reputation: 8246

depends on how you have set up your firebase apis.from flutter fire here is how you should set it up. In your app directory, edit web/index.html to add the following: `

<body>
    <script src="https://www.gstatic.com/firebasejs/7.5.0/firebase-app.js"></script>
    <script src="https://www.gstatic.com/firebasejs/7.5.0/firebase-firestore.js"></script>
     <!-- ADD THIS BEFORE YOUR main.dart.js SCRIPT -->

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: ""
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);

`

Upvotes: 1

Related Questions