Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fix the error 'Failed to load FirebaseOptions from resource. Check that you have defined values.xml correctly.' [closed]

E/flutter ( 6571): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(java.lang.Exception: Failed to load FirebaseOptions from resource. Check that you have defined values.xml correctly., Exception, Cause: null, Stacktrace: java.lang.Exception: Failed to load FirebaseOptions from resource. Check that you have defined values.xml correctly.
E/flutter ( 6571):  at io.flutter.plugins.firebase.core.FlutterFirebaseCorePlugin.lambda$optionsFromResource$4$io-flutter-plugins-firebase-core-FlutterFirebaseCorePlugin(FlutterFirebaseCorePlugin.java:207)
E/flutter ( 6571):  at io.flutter.plugins.firebase.core.FlutterFirebaseCorePlugin$$ExternalSyntheticLambda2.run(Unknown Source:4)
E/flutter ( 6571):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
E/flutter ( 6571):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)
E/flutter ( 6571):  at java.lang.Thread.run(Thread.java:1012)
E/flutter ( 6571): , null)
E/flutter ( 6571): #0      FirebaseCoreHostApi.optionsFromResource (package:firebase_core_platform_interface/src/pigeon/messages.pigeon.dart:242:7)
E/flutter ( 6571): <asynchronous suspension>
E/flutter ( 6571): #1      MethodChannelFirebase.initializeApp (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:89:25)
E/flutter ( 6571): <asynchronous suspension>
E/flutter ( 6571): #2      Firebase.initializeApp (package:firebase_core/src/firebase.dart:43:31)

Error throws right after calling await Firebase.initializeApp();

It's happening after removing imperative apply of Flutter's Gradle plugins which is deprecated. I.e. after migrate to the new, declarative apply issue started. Checked by creating new project also facing same issue.

settings.gradle file snap

App is building for iOS and Android not web.

Env: firebase_core: ^2.27.1

Flutter doctor result

like image 768
sanjay c Avatar asked Sep 03 '25 15:09

sanjay c


2 Answers

I found that after passing options it's working:

await Firebase.initializeApp(
  options: FirebaseOptions(
    apiKey: 'key',
    appId: 'id',
    messagingSenderId: 'sendid',
    projectId: 'myapp',
    storageBucket: 'myapp-b9yt18.appspot.com',
  )
);

Created new app, and configured it with CLI mode it will generate the options file for all platforms. Just add google-services.json won't be enough from flutter 3.19 it seems.

Firebase CLI config

Once the files are generated your Firebase init code should look like this. Options are applied through CLI generated options file according to platform.

await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform
);

However, your project distinguishes between test and production environments using the google-services.json file, so you might need to manually configure it accordingly.
Don't forget to also check the iOS version, because this configuration doesn't work on iOS 15.4. So you'll need to distinguish between platforms as well.

apiKey = google-service.json [api_key]
appId = google-service.json [mobilesdk_app_id]
messagingSenderId = google-service.json [project_number]
projectId = google-service.json [project_id]
    if (Platform.isAndroid) {
      FirebaseOptions firebaseOptions;
      if (environment == AppEnvironment.development) {
        firebaseOptions = const FirebaseOptions(
          apiKey: "DEV key",
          appId: "DEV ID",
          messagingSenderId: "DEV messagingSenderId",
          projectId: "DEV projectId",
        );
      } else {
        firebaseOptions = const FirebaseOptions(
           apiKey: "PUB key",
          appId: "PUB ID",
          messagingSenderId: "PUB messagingSenderId",
          projectId: "PUB projectId",
        );
      }
like image 117
sanjay c Avatar answered Sep 05 '25 05:09

sanjay c


I had a file firebase_options.dart which was created while configuring firebase for the project.

location of firebase_options.dart

So I did not need to create a new project. All I needed to do is to import this file in main.dart and pass FirebaseOptions to Firebase.initializeApp, like this

import 'package:flutter_complete_guide/firebase_options.dart';

and

Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform)

The currentPlatform getter of class DefaultFirebaseOptions (which is declared in firebase_options.dart) should look like this:

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.',
        );
    }
  }

Please look into your lib folder.

like image 27
Arthur Emig Avatar answered Sep 05 '25 07:09

Arthur Emig