Reputation: 39
So I have been trying to initialize my firebase with my flutter app but it keeps throwing an error every time, the code has no problem since flutter builds the app fine but just not firebase. So this is my code to initialize firebase;
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool _initialized = false;
bool _error = false;
void initializeFlutterFire() async {
try {
// Wait for Firebase to initialize and set `_initialized` state to true
await Firebase.initializeApp();
setState(() {
_initialized = true;
});
} catch (e) {
// Set `_error` state to true if Firebase initialization fails
setState(() {
_error = true;
});
}
}
@override
void initState() {
initializeFlutterFire();
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp();
and this is the error I keep getting:
Error: Assertion failed:
file:///home/pete/snap/flutter/common/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_core_web-1.4.0/lib/src/fire
base_core_web.dart:271:11
options != null
"FirebaseOptions cannot be null when creating the default app."
at Object.throw_ [as throw] (http://localhost:35305/dart_sdk.js:5061:11)
at Object.assertFailed (http://localhost:35305/dart_sdk.js:4986:15)
at firebase_core_web.FirebaseCoreWeb.new.initializeApp
(http://localhost:35305/packages/firebase_core_web/firebase_core_web.dart.lib.js:243:42)
at initializeApp.next (<anonymous>)
at http://localhost:35305/dart_sdk.js:38640:33
at _RootZone.runUnary (http://localhost:35305/dart_sdk.js:38511:59)
at _FutureListener.thenAwait.handleValue (http://localhost:35305/dart_sdk.js:33713:29)
at handleValueCallback (http://localhost:35305/dart_sdk.js:34265:49)
at Function._propagateToListeners (http://localhost:35305/dart_sdk.js:34303:17)
at _Future.new.[_completeWithValue] (http://localhost:35305/dart_sdk.js:34151:23)
at async._AsyncCallbackEntry.new.callback (http://localhost:35305/dart_sdk.js:34172:35)
at Object._microtaskLoop (http://localhost:35305/dart_sdk.js:38778:13)
at _startMicrotaskLoop (http://localhost:35305/dart_sdk.js:38784:13)
at http://localhost:35305/dart_sdk.js:34519:9
Upvotes: 3
Views: 15660
Reputation: 163
UPDATED: Dont waste time on configuring it by yourself, use this CLI https://firebase.flutter.dev/docs/overview/#using-the-flutterfire-cli
Following the steps will lead you to configure it for all the platforms. Its MAGIC!
Upvotes: 0
Reputation: 79
I was facing this issue so I just used this command and it was fixed. please try once before doing anything else
$ flutter upgrade
Upvotes: 0
Reputation: 183
I fixed the issue by the following steps:
Worked for me!
Upvotes: 12
Reputation: 2655
The firebase_options file is missing from your import statement and options is missing from the Firebase.initializeApp method.
Import the firebase_core plugin and firebase_options files.
//lib/main.dart
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
Add options parameter to the Firebase.initializeApp method inside the main function.
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(options:
DefaultFirebaseOptions.currentPlatform);
runApp(const MyApp());
}
Documentation for FlutterFire - initializing-flutterfire
Upvotes: 2