skip to Main Content

I have created new project in flutter and added firebase using flutterfire configure command after that i have initialized my project like below

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
  runApp(const MyApp());
}

then it started throwing error which below i mentioned

Exception has occurred.
"Error: Expected a value of type 'Object', but got one of type 'Null'
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 266:49      throw_
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 99:3        castError
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 452:10  cast
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 73:24                 as
packages/firebase_core_web/src/firebase_core_web.dart 85:20                                                                    get [_ignoredServiceScripts]
packages/firebase_core_web/src/firebase_core_web.dart 136:28                                                                   _initializeCore
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54                runBody
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 123:5                _async
packages/firebase_core_web/src/firebase_core_web.dart 128:31                                                                   [_initializeCore]
packages/firebase_core_web/src/firebase_core_web.dart 170:11                                                                   initializeApp
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54                runBody
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 123:5                _async
packages/firebase_core_web/src/firebase_core_web.dart 166:44                                                                   initializeApp
packages/firebase_core/src/firebase.dart 43:47                                                                                 initializeApp
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54                runBody
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 123:5                _async
packages/firebase_core/src/firebase.dart 39:43                                                                                 initializeApp
packages/predx/main.dart 8:18                                                                                                  main
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54                runBody
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 123:5                _async
packages/predx/main.dart 5:10                                                                                                  main$
web_entrypoint.dart 24:31                                                                                                      <fn>
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 334:14  _checkAndCall
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 339:39  dcall
C:/b/s/w/ir/cache/builder/src/out/host_debug/flutter_web_sdk/lib/ui/initialization.dart 77:15                                  <fn>
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 45:50                <fn>
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/zone.dart 1653:54                                              runUnary
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 147:18                                        handleValue
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 766:44                                        handleValueCallback
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 795:13                                        _propagateToListeners
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 566:5                                         [_completeWithValue]
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 639:7                                         callback
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/schedule_microtask.dart 40:11                                  _microtaskLoop
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/schedule_microtask.dart 49:5                                   _startMicrotaskLoop
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 166:15               <fn>
"

My flutter version

Flutter 3.3.10 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 135454af32 (8 days ago) • 2022-12-15 07:36:55 -0800
Engine • revision 3316dd8728
Tools • Dart 2.18.6 • DevTools 2.15.0

Firebase core version

firebase_core: ^2.4.0

I’m facing this issue only on web

4

Answers


  1. check your firebase_options.dart file in lib/ Folder does it contains web config like in here

    // File generated by FlutterFire CLI.
    // ignore_for_file: lines_longer_than_80_chars, avoid_classes_with_only_static_members
    import 'package:firebase_core/firebase_core.dart' show FirebaseOptions;
    import 'package:flutter/foundation.dart'
        show defaultTargetPlatform, kIsWeb, TargetPlatform;
    
    /// Default [FirebaseOptions] for use with your Firebase apps.
    ///
    /// Example:
    /// ```dart
    /// import 'firebase_options.dart';
    /// // ...
    /// await Firebase.initializeApp(
    ///   options: DefaultFirebaseOptions.currentPlatform,
    /// );
    /// ```
    class DefaultFirebaseOptions {
      static FirebaseOptions get currentPlatform {
        if (kIsWeb) {
          return web;
        }
        switch (defaultTargetPlatform) {
          case TargetPlatform.android:
            return android;
          case TargetPlatform.iOS:
            return ios;
          case TargetPlatform.macOS:
            return macos;
          case TargetPlatform.windows:
            throw UnsupportedError(
              'DefaultFirebaseOptions have not been configured for windows - '
              'you can reconfigure this by running the FlutterFire CLI again.',
            );
          case TargetPlatform.linux:
            throw UnsupportedError(
              'DefaultFirebaseOptions have not been configured for linux - '
              'you can reconfigure this by running the FlutterFire CLI again.',
            );
          default:
            throw UnsupportedError(
              'DefaultFirebaseOptions are not supported for this platform.',
            );
        }
      }
       //  web config
      static const FirebaseOptions web = FirebaseOptions(
        apiKey: 'AIzaSyB7wZb2tO1-Fs6GbDADUSTs2Qs3w08Hovw',
        appId: '1:406099696497:web:87e25e51afe982cd3574d0',
        messagingSenderId: '406099696497',
        projectId: 'flutterfire-e2e-tests',
        authDomain: 'flutterfire-e2e-tests.firebaseapp.com',
        databaseURL:
            'https://flutterfire-e2e-tests-default-rtdb.europe-west1.firebasedatabase.app',
        storageBucket: 'flutterfire-e2e-tests.appspot.com',
        measurementId: 'G-JN95N1JV2E',
      );
    
      static const FirebaseOptions android = FirebaseOptions(
        apiKey: 'AIzaSyCdRjCVZlhrq72RuEklEyyxYlBRCYhI2Sw',
        appId: '1:406099696497:android:175ea7a64b2faf5e3574d0',
        messagingSenderId: '406099696497',
        projectId: 'flutterfire-e2e-tests',
        databaseURL:
            'https://flutterfire-e2e-tests-default-rtdb.europe-west1.firebasedatabase.app',
        storageBucket: 'flutterfire-e2e-tests.appspot.com',
      );
    
      static const FirebaseOptions ios = FirebaseOptions(
        apiKey: 'AIzaSyDooSUGSf63Ghq02_iIhtnmwMDs4HlWS6c',
        appId: '1:406099696497:ios:0670bc5fe8574a9c3574d0',
        messagingSenderId: '406099696497',
        projectId: 'flutterfire-e2e-tests',
        databaseURL:
            'https://flutterfire-e2e-tests-default-rtdb.europe-west1.firebasedatabase.app',
        storageBucket: 'flutterfire-e2e-tests.appspot.com',
        androidClientId:
            '406099696497-17qn06u8a0dc717u8ul7s49ampk13lul.apps.googleusercontent.com',
        iosClientId:
            '406099696497-l9gojfp6b3h1cgie1se28a9ol9fmsvvk.apps.googleusercontent.com',
        iosBundleId: 'io.flutter.plugins.firebase.firestore.example',
      );
    
      static const FirebaseOptions macos = FirebaseOptions(
        apiKey: 'AIzaSyDooSUGSf63Ghq02_iIhtnmwMDs4HlWS6c',
        appId: '1:406099696497:ios:0670bc5fe8574a9c3574d0',
        messagingSenderId: '406099696497',
        projectId: 'flutterfire-e2e-tests',
        databaseURL:
            'https://flutterfire-e2e-tests-default-rtdb.europe-west1.firebasedatabase.app',
        storageBucket: 'flutterfire-e2e-tests.appspot.com',
        androidClientId:
            '406099696497-17qn06u8a0dc717u8ul7s49ampk13lul.apps.googleusercontent.com',
        iosClientId:
            '406099696497-l9gojfp6b3h1cgie1se28a9ol9fmsvvk.apps.googleusercontent.com',
        iosBundleId: 'io.flutter.plugins.firebase.firestore.example',
      );
    }
    
    Login or Signup to reply.
  2. Check if It’s configured in the firebase console

    Also, check the config in firebase_options.dart

    // Import the functions you need from the SDKs you need
    import { initializeApp } from "firebase/app";
    import { getAnalytics } from "firebase/analytics";
    // TODO: Add SDKs for Firebase products that you want to use
    // https://firebase.google.com/docs/web/setup#available-libraries
    
    // Your web app's Firebase configuration
    // For Firebase JS SDK v7.20.0 and later, measurementId is optional
    const firebaseConfig = {
      apiKey: "<your key>",
      authDomain: "<your project id>.firebaseapp.com",
      projectId: "<your project id>",
      storageBucket: "<your project id>.appspot.com",
      messagingSenderId: "<Messeging sender id>",
      appId: "<App id>",
      measurementId: "<Measurement id>"
    };
    
    // Initialize Firebase
    const app = initializeApp(firebaseConfig);
    const analytics = getAnalytics(app);
    
    Login or Signup to reply.
  3. I’m guessing that you have recently upgraded the flutter version from "Without Null Safety" to "With Null Safety". Please make sure that you have the latest version of Firebase and it’s packages. Also upgrade the version of flutter itself.

    If you don’t want to do the above, you could try modifying
    firebase_core/firebase_core_web/lib/src/interop/utils/utils.dart

    from

    dynamic dartify(
      Object? jsObject, [
      Object Function(Object? object)? customDartify,
    ])
    

    to

    dynamic dartify(
      Object? jsObject, [
      Object? Function(Object object)? customDartify,
    ])
    
    Login or Signup to reply.
  4. just verify the below step.

    check Main.dart file

    Future<void> main() async {
      WidgetsFlutterBinding.ensureInitialized();
      if(kIsWeb){
        await Firebase.initializeApp(
            options: const FirebaseOptions(
                apiKey: "",
                authDomain: "",
                projectId: "",
                storageBucket: "",
                messagingSenderId: "",
                appId: "",
                measurementId: "")
        );
      }else{
        await Firebase.initializeApp();
      }
      runApp(const MyApp());
    }
    

    NEXT checkpoint

    <script type="module">
      // Import the functions you need from the SDKs you need
      import { initializeApp } from "https://www.gstatic.com/firebasejs/9.17.1/firebase-app.js";
      import { getAnalytics } from "https://www.gstatic.com/firebasejs/9.17.1/firebase-analytics.js";
      // TODO: Add SDKs for Firebase products that you want to use
      // https://firebase.google.com/docs/web/setup#available-libraries
    
      // Your web app's Firebase configuration
      // For Firebase JS SDK v7.20.0 and later, measurementId is optional
      const firebaseConfig = {
        apiKey: "",
        authDomain: "",
        projectId: "",
        storageBucket: "",
        messagingSenderId: "",
        appId: "",
        measurementId: ""
      };
    
      // Initialize Firebase
      const app = initializeApp(firebaseConfig);
      const analytics = getAnalytics(app);
    </script>
    

    Next checkpoint

    <body>
    <!-- This script installs service_worker.js to provide PWA functionality to
         application. For more information, see:
         https://developers.google.com/web/fundamentals/primers/service-workers -->
    <script src="https://www.gstatic.com/firebasejs/8.10.1/firebase-app.js"></script>
    <script src="https://www.gstatic.com/firebasejs/8.10.1/firebase-firestore.js"></script>
    <script src="https://www.gstatic.com/firebasejs/8.10.1/firebase-anaylytics.js"></script>
    <script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-storage.js"></script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search