shadowsheep
shadowsheep

Reputation: 15012

How to choose between development and production firebase project based on build flavours?

In native development I switch my application between firebase production and development project depending on build "flavour".

On Android I put google-services.json inside the folder

<ProjectDir>/app/src/<BuildFlavour>

Where BuildFlavour could be, for example, debug or release.

So that could be easily done also for a Flutter project. Indeed I see:

> Task :app:processDebugGoogleServices
Parsing json file: /Users/shadowsheep/AndroidStudioProjects/flutter_app_test_fcm_messaging/android/app/src/debug/google-services.json

On iOS I'll do it this way instead:

        NSString *googleFirebaseJsonFileName = @"GoogleService-Info";
#ifdef DEBUG
        NSLog(@"[FIREBASE] Development mode.");
        googleFirebaseJsonFileName = @"GoogleService-Info-Debug";
#else
        NSLog(@"[FIREBASE] Production mode.");
#endif
        NSLog(@"%@", googleFirebaseJsonFileName);
        NSString *googleFirebaseJsonFilePath = [[NSBundle mainBundle]
                                                pathForResource:googleFirebaseJsonFileName
                                                ofType:@"plist"];
        NSLog(@"%@", googleFirebaseJsonFilePath);

        // https://firebase.google.com/docs/cloud-messaging/ios/client
        FIROptions *options = [[FIROptions alloc]
                               initWithContentsOfFile:googleFirebaseJsonFilePath];
        [FIRApp configureWithOptions:options];

How can I achieve this in Flutter the right way for iOS project? I've gotta put this exact code inside AppDelegate here?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [GeneratedPluginRegistrant registerWithRegistry:self];
  // Override point for customization after application launch.
    // I've to init Firebase here the same way?
  return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

Upvotes: 0

Views: 1107

Answers (2)

shadowsheep
shadowsheep

Reputation: 15012

Eventually I've done like that:

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [GeneratedPluginRegistrant registerWithRegistry:self];
  // Override point for customization after application launch.
    NSString *googleFirebaseJsonFileName = @"GoogleService-Info";
#ifdef DEBUG
    NSLog(@"[FIREBASE] Development mode.");
    googleFirebaseJsonFileName = @"GoogleService-Info-Debug";

    NSLog(@"%@", googleFirebaseJsonFileName);
    NSString *googleFirebaseJsonFilePath = [[NSBundle mainBundle]
                                            pathForResource:googleFirebaseJsonFileName
                                            ofType:@"plist"];
    NSLog(@"%@", googleFirebaseJsonFilePath);

    // https://firebase.google.com/docs/cloud-messaging/ios/client
    FIROptions *options = [[FIROptions alloc]
                           initWithContentsOfFile:googleFirebaseJsonFilePath];
    if ([FIRApp defaultApp]) {
        NSLog(@"Firebase already configured!");
        [[FIRApp defaultApp]
         deleteApp:^(BOOL success) {
             if (success) {
                 NSLog(@"Reconfigure Firebase");
                 [FIRApp configureWithOptions:options];
             }
         }];
    } else {
        [FIRApp configureWithOptions:options];
    }
#else
    NSLog(@"[FIREBASE] Production mode.");
#endif

  return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

It seems to work.

Upvotes: 1

tudorprodan
tudorprodan

Reputation: 965

You can have flavours in Flutter by having different entry points.

For example you can have an enum like this:

enum BuildConfig { development, production, }

And you can have two entry files for your application.

One for production:

main_production.dart

Which at some point it inits a constant somewhere like:

buildConfig = BuildConfig.production;

And another file for development:

main_dev.dart

Which will init the constant like:

buildConfig = BuildConfig.development;

You can start your app using the -t flag (for target)

flutter run -t lib/main_production.dart

In this context the buildConfig var will have the value BuildConfig.production.

I highly recommend checking out this article for more info.

Based in this you can choose what Firebase project to init.

Upvotes: 0

Related Questions