0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

flutterのconst String.fromEnvironmentの値によってコンパイルが通らなくなる問題

Posted at

ステージング環境でビルドしようとすると、唐突にコンパイルが通らなくなった。
原因が分かるまでちょっと時間が掛かったのでメモっておきます。

デプロイ先によって設定値を切り替えたい場合は、--dart-define-from-fileを使用するのが便利です。

こんな感じで環境別に設定ファイルを準備します。

dev.json
{
    "api": "https://dev.example.com/v1/",
    "key": "something-dev-key"
}
stg.json
{
    "api": "https://stg.example.com/v1/",
    "key": "something-stg-key"
}

使う方は、こんな感じになります。

main.dart
void main {
    var api = const String.fromEnvironment("api");
    var key = const String.fromEnvironment("key");
}

ビルドする時は、以下の様に使用する設定ファイルを指定する

flutter build windows --dart-define-from-file=path/to/stg.json

ここまでは、特に問題無いと思います。
今回、ステージングを指定すると何故かコンパイルが通らない。
keyの値を変更すると何故かコンパイルが通る。
何か文字が原因らしいので、軽く確認した範囲では「${<」当たりの文字があるとコンパイルが通らなくなる。
今回は諸事情で、使用できる文字だけにするのが難しかったので別の方法で対応することにしました。

結果

使えない文字のある値をbase64でエンコードし実行時にデコードする方法を採用しました。
頻繁に使用する場合は、アプリ起動時の初期化でキャッシュするなりすればよいと思います。

var key = base64.decode(const String.fromEnvironment("key"));

ちなみに、この問題は他環境のmacやlinuxでは、普通にコンパイルできるようなのでwindows環境限定の問題っぽいです。
というか、恐らくバグですね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?