0

I have a Flutter project linked to my Firebase account (using FlutterFire CLI).
Now that I finished the project I want to sell the source code.
How can I change the Firebase account linked to this project?

When using FlutterFire CLI, it generates a file called firebase_options.dart containing Firebase options for each platform like this:

static const FirebaseOptions android = FirebaseOptions(  
 apiKey: '-------',  
 appId: '------',  
 messagingSenderId: '-----',  
 projectId: '----------',  
 storageBucket: '--------',  
  );

Do I have just to change these values? If yes, where can I find all of them?

main.dart

import 'firebase_options.dart';


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

  runApp(const MyApp());
}

Full firebase_options.dart file

// File generated by FlutterFire CLI.
// ignore_for_file: lines_longer_than_80_chars, avoid_classes_with_only_static_members
import 'package:firebase_core/firebase_core.dart' show FirebaseOptions;
import 'package:flutter/foundation.dart'
    show defaultTargetPlatform, kIsWeb, TargetPlatform;

/// Default [FirebaseOptions] for use with your Firebase apps.
///
/// Example:
/// ```dart
/// import 'firebase_options.dart';
/// // ...
/// await Firebase.initializeApp(
///   options: DefaultFirebaseOptions.currentPlatform,
/// );
/// ```
class DefaultFirebaseOptions {
  static FirebaseOptions get currentPlatform {
    if (kIsWeb) {
      return web;
    }
    switch (defaultTargetPlatform) {
      case TargetPlatform.android:
        return android;
      case TargetPlatform.iOS:
        throw UnsupportedError(
          'DefaultFirebaseOptions have not been configured for ios - '
          'you can reconfigure this by running the FlutterFire CLI again.',
        );
      case TargetPlatform.macOS:
        throw UnsupportedError(
          'DefaultFirebaseOptions have not been configured for macos - '
          'you can reconfigure this by running the FlutterFire CLI again.',
        );
      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.',
        );
    }
  }

  static const FirebaseOptions web = FirebaseOptions(
    apiKey: '------',
    appId: '------',
    messagingSenderId: '-----',
    projectId: '------',
    authDomain: '--------',
    storageBucket: '--------',
    measurementId: '--------',
  );

  static const FirebaseOptions android = FirebaseOptions(
    apiKey: '----',
    appId: '----',
    messagingSenderId: '--------',
    projectId: '------',
    storageBucket: '--------',
  );
}

I tried to change the values (in firebase_options.dart) and that caused this error:

E/flutter (26545): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: [core/duplicate-app] A Firebase App named "[DEFAULT]" already exists

it throws this error(duplicateApp from method_channel_firebase.dart):

// If there is a native default app and the user provided options do a soft
// check to see if options are roughly identical (so we don't unnecessarily
// throw on minor differences such as platform specific keys missing
// e.g. hot reloads/restarts).
if (defaultApp != null && _options != null) {
  if (_options.apiKey != defaultApp.options.apiKey ||
      (_options.databaseURL != null &&
          _options.databaseURL != defaultApp.options.databaseURL) ||
      (_options.storageBucket != null &&
          _options.storageBucket != defaultApp.options.storageBucket)) {
    // Options are different; throw.
    throw duplicateApp(defaultFirebaseAppName);
  }
  // Options are roughly the same; so we'll return the existing app.
}
Aymen Dennoub
  • 724
  • 6
  • 10

1 Answers1

0

You can change the firebase account linked to this project. This error that you are seeing

E/flutter (26545): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: [core/duplicate-app] A Firebase App named "[DEFAULT]" already exists

is caused by the original gradle sync operation generating a Google-Services.json file and using it within the app. To remove this error, switch into the android folder from your main flutter project and run

./gradlew clean

This command calls the gradle wrapper which then cleans the project and brings it back to its original state. The flutter team uses a default empty flutter_options.dart file for their projects. You may want to consider doing that if you are seeing the source code to someone. I would just package instructions for how to configure their project to work with a new Firebase project that they would select.

Alexander N.
  • 1,458
  • 14
  • 25