将现有项目迁移到WebApp + Firebase(Firestore +托管)

时间:2020-03-29 08:07:37

标签: flutter google-cloud-firestore firebase-hosting flutter-web

我有一个使用Firestore的现有Flutter应用程序(可在Android / IOS上正确运行)。

目标:

  1. 我想首先让它支持网络
  2. 我想将其托管在Firebase托管上

到目前为止的结果:

  1. 在本地空白页中运行
  2. 部署到Firebase时,甚至似乎都没有运行index.html

我尝试过的事情

  1. 根据this教程:

    a)在我的Firebase项目中添加了一个Web应用

    b)npm install -g firebase-tools

    c)firebase login

    d)firebase init

    ==>我选择Firestore and Hosting

    ==>使用现有项目,选择我的项目

    ==>使用所有默认设置(公共目录的build / web文件夹除外)

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

    f)flutter create .

    g)flutter build web

    h)然后,我将所有内容从build / web复制到/ web文件夹,从firebase的底部添加CDN,然后通过以下方式调用Firebase JS SDK:

错误

在chrome上本地运行时出现此错误:

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

关于使用firebase deploy的页面,页面显示我成功设置了主机,但未显示index.html

供参考:

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

1 个答案:

答案 0 :(得分:1)

取决于您如何设置Firebase api。从flutter fire开始,这里是您应该如何设置它的方法。 在您的应用目录中,编辑web / index.html以添加以下内容: `

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

`