Setting environment variables in Flutter

后端 未结 4 1620
野趣味
野趣味 2020-12-16 09:24

For example, building a client for an API, like Twitch.

In a Dart CLI binary, I could use a generic environment variable, or a Dart definition variable. For example,

4条回答
  •  臣服心动
    2020-12-16 10:18

    Starting from Flutter 1.17 you can define compile-time variables if you want to.

    To do so just use --dart-define argument during flutter run or flutter build

    If you need to pass multiple key-value pairs, just define --dart-define multiple times:

    flutter run --dart-define=SOME_VAR=SOME_VALUE --dart-define=OTHER_VAR=OTHER_VALUE

    and then, anywhere in your code you can use them like:

    const SOME_VAR = String.fromEnvironment('SOME_VAR', defaultValue: 'SOME_DEFAULT_VALUE');
    const OTHER_VAR = String.fromEnvironment('OTHER_VAR' defaultValue: 'OTHER_DEFAULT_VALUE');
    

    Also, they can be used in native layers too.

    If something - here is an article that explains more

提交回复
热议问题