0

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
3
  • Android Studio merges resources from different flavor directories. Ensure that your google-services.json is not being overwritten during the build process. You can verify this by checking the merged google-services.json in the build output directory. Commented Dec 15, 2023 at 15:29
  • I do not find google-services.json in build output directory, where should i find it exactly ? Commented Dec 15, 2023 at 15:42
  • What should I do to avoid this merge ? Commented Dec 15, 2023 at 15:48

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.