Skip to content

Instantly share code, notes, and snippets.

@tomasbaran
Last active October 4, 2023 16:42
Show Gist options
  • Save tomasbaran/04e68c5d912447ceed2715b9f225e604 to your computer and use it in GitHub Desktop.
Save tomasbaran/04e68c5d912447ceed2715b9f225e604 to your computer and use it in GitHub Desktop.
flavors config
WEB CONFIG
flavor.dart:
```
import 'package:flutter/foundation.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:today/models/enums.dart';
class Flavor {
static FlavorType? selectedFlavor;
static Future<FlavorType> selected() async {
// cache performance enhancement
// if (_selected != null) {
// return _selected!;
// }
final packageInfo = await PackageInfo.fromPlatform();
await dotenv.load(fileName: "lib/dotenv");
if (packageInfo.packageName == dotenv.get('PACKAGE_NAME_DEV')) {
selectedFlavor = FlavorType.dev;
} else if (packageInfo.packageName == dotenv.get('PACKAGE_NAME_LIVE')) {
selectedFlavor = FlavorType.live;
// web
} else if (kIsWeb) {
const environmentParameter = String.fromEnvironment('FLAVOR');
switch (environmentParameter) {
case 'dev':
selectedFlavor = FlavorType.dev;
case 'live':
selectedFlavor = FlavorType.live;
default:
throw Exception("Unknown environment $environmentParameter");
}
}
return selectedFlavor!;
}
}
```
app_state.dart:
```
Future<void> initializeSelectedFlavor() async {
final selectedFlavor = await Flavor.selected();
debugPrint("Connecting to ${selectedFlavor.name} environment (${selectedFlavor.baseUrl})...");
await dotenv.load(fileName: "lib/dotenv");
switch (selectedFlavor) {
case FlavorType.dev:
await Firebase.initializeApp(name: kIsWeb ? null : 'dev', options: dev.DefaultFirebaseOptions.currentPlatform);
await MixpanelService.initMixpanel(dotenv.get('MIXPANEL_TOKEN_DEV'));
break;
case FlavorType.live:
await Firebase.initializeApp(name: kIsWeb ? null : 'live', options: live.DefaultFirebaseOptions.currentPlatform);
await MixpanelService.initMixpanel(dotenv.get('MIXPANEL_TOKEN_LIVE'));
break;
default:
throw Exception("Unknown environment $selectedFlavor");
}
listenToAuthChanges();
}
```
launch.json:
```
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "dev (debug mode)",
"request": "launch",
"type": "dart",
"args": [
"--flavor",
"dev",
"--dart-define",
"FLAVOR=dev"
]
},
{
"name": "dev (profile mode)",
"request": "launch",
"type": "dart",
"flutterMode": "profile",
"args": [
"--flavor",
"dev",
"--dart-define",
"FLAVOR=dev"
]
},
{
"name": "dev (release mode)",
"request": "launch",
"type": "dart",
"flutterMode": "release",
"args": [
"--flavor",
"dev",
"--dart-define",
"FLAVOR=dev"
]
},
{
"name": "live (debug mode)",
"request": "launch",
"type": "dart",
"args": [
"--flavor",
"live",
"--dart-define",
"FLAVOR=live"
]
},
{
"name": "live (profile mode)",
"request": "launch",
"type": "dart",
"flutterMode": "profile",
"args": [
"--flavor",
"live",
"--dart-define",
"FLAVOR=live"
]
},
{
"name": "live (release mode)",
"request": "launch",
"type": "dart",
"flutterMode": "release",
"args": [
"--flavor",
"live",
"--dart-define",
"FLAVOR=live"
]
},
]
}
```
FIREBASE CONFIG
# 1. Create flavor firebase project (e.g. app-dev) by going to firebase console
# 2. Add firebase to flutter app with CLI instructions by changing `my-app` & also `firebase_options_dev`:
```
flutterfire config \
--project=my-app-dev \
--out=lib/firebase_options_dev.dart \
--ios-bundle-id=com.company.my-app.dev \
--macos-bundle-id=com.company.my-app.dev \
--android-package-name=com.company.my-app.dev
```
when uploading to dev firebase hosting:
1. flutter build web --dart-define FLAVOR=dev
2. firebase use dev
3. firebase deploy
when uploading to live firebase hosting:
1. flutter build web --dart-define FLAVOR=live
2. firebase use live
3. firebase deploy
# src: https://yfujiki.medium.com/best-practice-with-flavors-in-flutter-development-16d2cbbf5959
# alt src: https://codewithandrea.com/articles/flutter-flavors-for-firebase-apps/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment