Firebase Flutter flavor does not work with Android Studio

261 views Asked by At

When I run flutter run --flavor dev -t lib/main_dev.dart, I still have the data from my Firebase "prod" project instead of "dev".

  1. With Android Studio, I have created two configurations :
  • main_dev.dart with dev build flavor
  • main_prod.dart with prod build flavor
  1. build.gradle :

    flavorDimensions "default" productFlavors { dev { dimension "default" applicationIdSuffix ".dev" } prod { dimension "default" } }

  2. Distinct google-services.json file in two different folders :

  • app/src/dev
  • app/src/prod
  1. Distinct firebase_options.dart in two different folders :
  • lib/dev
  • lib/firebase/prod
  1. Flutter clean
2

There are 2 answers

0
Chloé On BEST ANSWER

The link of firebase_options.dart was the one from prod instead of dev in main_dev.dart.

0
La Pyae On

I am not clear your your problem. Because can't see detail of your project structure. As for me I build with flavor as below code and suppose this will help for you.

Firstly build two main entry file for prod and dev.

for PROD flavor

void main() async {
  FlavorConfig(
    flavor: Flavor.prod,
    flavorEnvironment: "Production",
    appTitle: "Production",
    flavorValues: FlavorValues(
      appId: "com.example.production",
      baseUrl: BASE_PRODUCTION_URL,
    ),
  );
  mainPrimary();
}

for DEV flavor

void main() async {
  FlavorConfig(
    flavor: Flavor.staging,
    flavorEnvironment: "Dev",
    appTitle: "Dev",
    flavorValues: FlavorValues(
      appId: "com.example.dev",
      baseUrl: BASE_STAGING_URL,
    ),
  );
  mainPrimary();
}

for main file, the entry of project

void mainPrimary() async {
  runApp(MyApp());
}

for FlavorConfig file,

class FlavorConfig {
  final Flavor flavor;
  final FlavorValues flavorValues;
  final String environment;
  final String appTitle;
  static FlavorConfig? _instance;

  factory FlavorConfig(
      {required Flavor flavor,
      required FlavorValues flavorValues,
      required String flavorEnvironment,
      required String appTitle,}) {
    _instance = FlavorConfig._internalConstructor(
      flavor: flavor,
      flavorValues: flavorValues,
      environment: flavorEnvironment,
      appTitle: appTitle,
    );
    return _instance!;
  }

  FlavorConfig._internalConstructor({
    required this.flavor,
    required this.flavorValues,
    required this.environment,
    required this.appTitle
  });

  static FlavorConfig? get instance => _instance;

  static bool _isStagingApp() => _instance?.flavorValues == Flavor.staging;

  static bool _isProdApp() => _instance?.flavorValues == Flavor.prod;
}

class FlavorValues {
  final String? appId;
  final String? baseUrl;
  final String? basicToken;

  FlavorValues({
    this.appId,
    this.baseUrl,
    this.basicToken,
  });
}

enum Flavor { prod, staging }

To change the data depend on flavor, use like that

child: GetMaterialApp(
        title: FlavorConfig.instance!.appTitle,
      ),

As native flavor configure in app/build.grade like that

    flavorDimensions "Sample App"

    productFlavors{
        staging{
            dimension "sampleApp"
            applicationId "com.example.staging"
            versionCode 1
            versionName "1.0"
        }
        prod{
            dimension "sampleApp"
            applicationId "com.example.production"
            versionCode 1
            versionName "1.0"
        }
    }

To run project just type with below command

flutter run --flavor dev -t lib/main_dev.dart

You can set google-services.json for different flavor in by adding in DEV and PROD file.

If above instruction can't help you. You should double check the appId while building project in firebase.