'Initializing FIrebase on Flutter throws error

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


Solution 1:[1]

The firebase_options file is missing from your import statement and options is missing from the Firebase.initializeApp method.

  1. Import the firebase_core plugin and firebase_options files.

     //lib/main.dart
     import 'package:firebase_core/firebase_core.dart';
     import 'firebase_options.dart';
    
  2. 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

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 Sharon