10

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
  • 121
  • 1
  • 1
  • 4

4 Answers4

32

Previously you had to download the google-service.json and GoogleService-Info.plist files 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 project within Flutter to automate the setup which adds the necessary libraries and files to android/ and ios/ for you.

  1. Create project 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 project
  4. run flutterfire configure

This will start a command line interface for you to select the Firebase project you want to link to the Flutter project. 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());
} 
Bugzilla
  • 1,856
  • 1
  • 10
  • 16
  • 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
8

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 Atim
  • 1,777
  • 16
  • 12
  • I had missed to run the command: `flutter pub add firebase_core`, and looking back at the Google Docs, this step wasn't mentioned in the ones I used either... – Boommeister Jul 10 '22 at 18:40
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