我想将firebase连接到我的flutter web应用程序。我遵循了网上、官方和建议的所有解决方案。
我的flutter版本:flutter 2.6.0-12.0.pre.522
我总是有这个错误在控制台:
Uncaught SyntaxError: Cannot use import statement outside a module
index.html中的代码来自build/web:(我的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文件夹只有一个host .(numbers).cache
重火力点。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
我遇到了同样的问题,但我通过使用以下更新的方法解决了这个问题。(我用的是最新的包)
看起来一些脚本标签不再需要了。
https://firebase.flutter.dev/docs/overview
https://firebase.flutter.dev/docs/manual-installation/
配置信息放在
Firebase.initializeApp (options: FirebaseOptions(...))
在省道码一侧。