【问题标题】:Flutter web app & firebase : Cannot use import statement outside a moduleFlutter web app & firebase:不能在模块外使用 import 语句
【发布时间】:2021-10-29 13:56:03
【问题描述】:

我想将 Firebase 连接到我的 Flutter Web 应用。 我遵循了网络、官方和建议上的所有解决方案。

我的 Flutter 版本:Flutter 2.6.0-12.0.pre.522

我在控制台中总是出现这个错误:

Uncaught SyntaxError: Cannot use import statement outside a module

来自 build/web 的 index.html 中的代码: (我的 Firebase 版本:9.21.0)

<body>
<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-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-firestore.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-storage.js"></script>
<script>
import firebase from 'firebase/compat/app';
import 'firebase/compat/auth';
import 'firebase/compat/firestore';
const firebaseConfig = {
   apiKey: "",
   authDomain: "",
   projectId: "",
   storageBucket: "",
   messagingSenderId: "",
   appId: ""
};
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
</script>

main.dart

import 'package:firebase_core/firebase_core.dart';
Future<void> main() async {
   WidgetsFlutterBinding.ensureInitialized();
   await Firebase.initializeApp();
   runApp(MyApp());
}

firebase 文件夹只有一个 hosts.(numbers).cache

firebase.json:

{"hosting": {
"public": "build/web",
"ignore": [
"firebase.json",
   "**/.*",
   "**/node_modules/**"
],
"rewrites": [
   {
      "source": "**",
      "destination": "/index.html"
   }
]
}
}

还有我的 pub spec.yaml:

dependencies:
   flutter:
      sdk: flutter
   firebase_core: ^1.8.0
   firebase_auth: ^3.1.4

【问题讨论】:

  • 你的问题解决了吗?

标签: firebase flutter import module


【解决方案1】:

我遇到了同样的问题,但我通过使用以下更新的方法解决了它。 (我使用了最新的包。)
似乎不再需要一些脚本标签。
https://firebase.flutter.dev/docs/overview
https://firebase.flutter.dev/docs/manual-installation/

配置信息放入

Firebase.initializeApp (options: FirebaseOptions(...)) 

在飞镖代码方面。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-09
    • 2021-08-06
    • 2021-05-09
    • 2020-01-27
    • 2020-02-11
    • 2020-12-11
    相关资源
    最近更新 更多