Flutter multi environment
Possible solution to use android run configurations with.
The schema will be next:
- create env files
- create different run android configuration (dev, stage, prod)
- save android run configurations to project
- write script to update android run configurations from .env files
This schema faced all requirements except "possible to run from Xcode". In common it still possible to run project from Xcode but to change configuration inside xcode to other env it still necessary to run project from android run configuration.
There more information and code example:
Nikita Filimonau
Updated on December 19, 2022Comments
-
Nikita Filimonau over 1 year
Hello everyone, I need some help with multi environment in flutter.
Expected result:
- multi environments (like prod / dev / staging)
- easy for change and visually separated (it could be 3 diff or one file with good structure)
- variables could be used in native code
- support of Android Studio flutter run
- possible to run from Xcode
Quite easy, right? ) No!
What I have tried:
- flutter_dotenv - not support native code
- flutter_config - it was quite good, but to run flutter with env you should write
ENFILE=.env.dev flutter run ...
, so with this approach you loose advantages of Flutter run console in android studio and flutter app not rerun automatically --dart-define=
approach - long lines of run command, hard to read, hard to change, code repetition forflutter run
andflutter build
It looks like I have missed something, I have come from react-native ant all this question resolved by
react-native-config
and command inpackage.json
but in flutter I don't see ways for predefined command like in package json (onlyRun/Debug Configurations
). But flutter run don't support env files but just sh scripts possible run with ENFILE but they don't trigger the flutter run console.I will be grateful for your help ))
-
Nikita Filimonau about 3 yearsHave you read my question? Third point!!!!
--dart-define= approach
My command looks like this: flutter run --flavor dev --dart-define=FLUTTER_APP_ADD_MOB_APP_ID_IOS=ca-app-pub-3940256099942544~2594085930 --dart-define=FLUTTER_APP_ADD_MOB_APP_ID_ANDROID=ca-app-pub-3940256099942544~4354546703 --dart-define=FLUTTER_APP_ADD_MOB_BANNER_IOS=ca-app-pub-3940256099942544/4339318960 --dart-define=FLUTTER_APP_ADD_MOB_BANNER_ANDROID=ca-app-pub-3940256099942544/8865242552 Don't worry this is test keys )) If you have good way to structure that, please provide that!