Flutter Flavoring — Android project using Firestore.
While developing an application, we can create different environments meant for developers, testers, and real users. Creating such an environment is the flavoring of an application.
Suppose, you have different environments having endpoints devapi.XYZ.com, testapi.XYZ.com and prodapi.XYZ.com . Now instead for hardcoding this values in our application, we can configure it to build app pointing to corresponding endpoints.
Let’s check how can we add flavor to flutter android app using cloud fire store.
How to configure a cloud firestore? please refer this link
Step 1: Create Flutter project (using name : add_flutter_app_to_firebase )
Step 2: We need to add following lines(shown in bold below) of code in android/app/build.gradle file.
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
As you can see in above code, applicationId for this project is “com.yogita.add_flutter_app_to_firebase”. In productFlavours, we have added development, staging and production environment. applicationIdSuffix for these flavors “.dev” , “.staging”. Just keeping null for production environment, these extensions are added to applicationId while integrating android project with firebase flavor project (more details in step 3)
Step 3: Create Firebase projects for different flavors
a. FlutterAppToFirebase — Dev for development flavor
In firebase project FlutterAppToFirebase — Dev, I have added “com.yogita.add_flutter_app_to_firebase.dev” as package name (repeat same for staging)
b. FlutterAppToFirebase — Staging for staging flavor
c. FlutterAppToFirebase for production.
for production applicationIdSuffix null so it take default package name “com.yogita.add_flutter_app_to_firebase”
Refer this link to learn more about creating firebase project and integration with flutter app.
Step 4: Now download google-service.json file for each flavor from firebase project (refer screenshot above)
In android/src folder, create a separate directory for each flavor and store the google-service.json file in respective folders.(see project structure below)
It’s really important to store google-service.json file under separate folder, based on arguments used in flutter run, corresponding . json file is called and connects with respective firebased project.
Step 5: Now are ready with flavoring and we can start playing with different flavors executing below commands.
flutter run --flavor <flavor-name>
flavor-name can be development, staging, production as per our choice.