Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter multi environment

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 for flutter run and flutter 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 in package.json but in flutter I don't see ways for predefined command like in package json (only Run/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 ))

like image 742
Nikita Filimonau Avatar asked Jul 23 '26 22:07

Nikita Filimonau


1 Answers

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:

  • Flutter Multi Environment with dart-define
  • GitHub code example
like image 104
Nikita Filimonau Avatar answered Jul 26 '26 11:07

Nikita Filimonau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!