'Flutter web app & firebase : Cannot use import statement outside a module
I want to connect firebase to my flutter web app. I followed all solutions on web, official and advice.
my flutter version : Flutter 2.6.0-12.0.pre.522
I have always this error inside console :
Uncaught SyntaxError: Cannot use import statement outside a module
the code inside index.html from build/web : (my firebase version : 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>
the main.dart
import 'package:firebase_core/firebase_core.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
the firebase folder have only an hosting.(numbers).cache
the firebase.json :
{"hosting": {
"public": "build/web",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
}
and my pub spec.yaml:
dependencies:
flutter:
sdk: flutter
firebase_core: ^1.8.0
firebase_auth: ^3.1.4
Solution 1:[1]
I ran into the same issue, but i solved it by using the following updated method. (I used the latest package.)
It looks like some script tags aren't needed anymore.
https://firebase.flutter.dev/docs/overview
https://firebase.flutter.dev/docs/manual-installation/
The config information is put in
Firebase.initializeApp (options: FirebaseOptions(...))
on the dart code side.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | M.M. |
