'Missing firebase_options.dart file in course "Get to know Firebase for Flutter"

I am using the flutter course "Get to know Firebase for Flutter" from https://firebase.google.com/codelabs/firebase-get-to-know-flutter#4.

I am in step_02 and I have added the following recommended code from stage 5.


import 'package:firebase_auth/firebase_auth.dart'; // new
import 'package:firebase_core/firebase_core.dart'; // new
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:provider/provider.dart';           // new

import 'firebase_options.dart';                    // new
import 'src/authentication.dart';                  // new
import 'src/widgets.dart';

Later in this stage there is a Test it section. However it fails because there is no firebase_options.dart file. How do I generate this file.

Thank you.



Solution 1:[1]

Previously you had to download the google-service.json and GoogleService-Info.plist from the Firebase console and place them in the android and ios folders in your Flutter app.

Starting with Flutter 2.8, there is a new way to initialize a Firebase proyect within Flutter to bypass this step.

  1. Create proyect in Firebase console, but you don't need to download the files mentioned or change build.gradle files
  2. Install Firebase CLI here
  3. run dart pub global activate flutterfire_cli in your Flutter proyect
  4. run flutterfire configure

This will start a command line interface for you to select the Firebase proyects you want to link to the Flutter proyect. After you complete this, a firebase_options.dart file will be generated in your lib/ folder.

Finally, to initialize Firebase in your main.dart:

import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';

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

PD: After updating to the new initialization method, Crashlytics and Analytics stopped working for me and I had to revert to the old initialization method. I don't think this new method is quite there yet. Guide for the old method.

Solution 2:[2]

The firebase_option file automatically generates after Flutter successfully configures your firebase project with your flutter app. For Android, make sure you've added the google-services.json file to your Android>app root directory and for ios, GoogleService-info.plist file into the root of your Xcode project as well as all targets.

If you're still having problems, I'd suggest using the Firebase CLI direct from the terminal to configure your firebase project.

  1. From your project root terminal, command:

      $ flutterfire configure   
        // This requires the Firebase CLI to work.
    
  2. Select firebase project by hitting return or enter. Next you'll be asked to select which platforms the configuration should support, e.g. android, ios, web. If you haven't created some of these in the firebase console, don't worry as it will create and register it for you in this step and update the android build.gradle files.

** Proceed to step 4 if you already have the firebase_core plugin installed. **

  1. Install the latest version of the firebase_core plugin by running this command from your project root directory:

     $ flutter pub add firebase_core  
    
  2. Add imports to the main file:

     import 'package:firebase_core/firebase_core.dart'; // 
     import 'firebase_options.dart'; // Generated file
    
  3. Update your main function to initialize firebase with this async function:

      Future<void> main() async {
       WidgetsFlutterBinding.ensureInitialized();
       await Firebase.initializeApp(options: 
       DefaultFirebaseOptions.currentPlatform);
       runApp(const YourAppName());
      }
    
  4. Delete the google-services.json and google.plist files if you had these installed before.

  5.    $ flutter clean 
       $ flutter run
    

See the FlutterFire documentation for more information.

Solution 3:[3]

After completing the instructions provided by Bugzilla, I was able to locate the firebase_options.dart file in the lib directory. I changed the path of the import from 'firebase_options.dart' to '../firebase_options.dart' and it worked for me.

Solution 4:[4]

Solved this by removing my existing Firebase project and creating a new one, disabling Google Analytics.

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
Solution 2
Solution 3 David C.
Solution 4 Kamox