skip to Main Content

I have created a music app using flutter I want to store user favorites and recently played songs in Firebase fire store so provide the code to store the data in fire store an I am getting error:

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.

2

Answers


  1. Include additional options when initializing the app. You can find the necessary keys in the Firebase console under project settings.

    Future<void> main() async {
     WidgetsFlutterBinding.ensureInitialized();
     await Firebase.initializeApp(
      options: FirebaseOptions(
              apiKey: 'your_api_key',
              appId: 'your_app_id',
              messagingSenderId: 'your_messaging_sender_id',
              projectId: 'your_project_id',
              storageBucket: 'your_storage_bucket',
            )
          );
    runApp(MyApp());
    }
    
    Login or Signup to reply.
  2. Make sure you have this file:

    Also Add this to assets in pubspec.yaml file.

    project_applicationandroidappgoogle-services.json

    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
    
      // Load the google-services.json file as a string
      String googleServicesJsonString =
          await rootBundle.loadString('android\app\google-services.json');
      print("google_Services_Json_String: $googleServicesJsonString");
    
      // Parse the JSON String into a JSON
      final googleServicesJson = jsonDecode(googleServicesJsonString);
      final projectId = googleServicesJson['project_info']["project_id"];
      print("projectId: $projectId");
      final messagingSenderId =
          googleServicesJson['project_info']["project_number"];
      print("messagingSenderId: $messagingSenderId");
      final storageBucket = googleServicesJson['project_info']["storage_bucket"];
      print("storageBucket: $storageBucket");
      final appId =
          googleServicesJson['client'][0]["client_info"]["mobilesdk_app_id"];
      print("appId: $appId");
      final apiKey = googleServicesJson['client'][0]["api_key"][0]["current_key"];
      print("apiKey: $apiKey");
    
      // Create a FirebaseOptions object from the extracted configuration
      FirebaseOptions options = FirebaseOptions(
        apiKey: apiKey,
        appId: appId,
        messagingSenderId: messagingSenderId,
        projectId: projectId,
        storageBucket: storageBucket,
      );
    
      // Initialize Firebase with the options
      await Firebase.initializeApp(options: options);
    
      print('Firebase app is initialized!');
    
      runApp(MyApp());
    }
    

    Remove print statements if you want.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search