2

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.

ubnewb
  • 41
  • 1
  • 3

4 Answers4

8

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.

Bugzilla
  • 926
  • 1
  • 7
  • 11
  • Yes this produced the options file, Can you recommend an existing flutter project that contains simple firebase CRUD coding. Thank you. – ubnewb Dec 18 '21 at 18:01
  • I didn't try it but seems good enough: [Blog post tutorial](https://blog.codemagic.io/integrate-firebase-cloud-firestore-with-flutter-perform-crud/) and [Github repo](https://github.com/sbis04/flutterfire-samples/tree/crud-firestore). They use the old innitialization but the code is not outdated – Bugzilla Dec 18 '21 at 23:07
  • I have tried to use the sample project with both the old initialization and the new initialization procedures. I am getting various errors. For example, the following error message for a file that is definitely in the correct location. File google-services.json is missing. The Google Services Plugin cannot function without it. Searched Location: /home/r/flutterfire-samples-crud-firestore/android/app/src/debug/google-services.json.... There should be a google supplied simple flutter firebase sample that only requires some unique identification to be added to it. – ubnewb Dec 20 '21 at 14:52
  • I had to move to new method as old method stopped working after I upgraded my flutter/Android/SDK versions. When will Crashlytics and Analytics support new method, any idea? (I got this error with old method - https://stackoverflow.com/q/71221368/1060044) – Krishna Shetty Feb 25 '22 at 07:07
1

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.

Sharon
  • 171
  • 2
  • 5
0

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.

David C.
  • 17
  • 3
0

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

Kamox
  • 1