Managing.env
like files is a means of preventing hard-coded credentials and separating environments.
It is good to implement multiple.env
files, e.g..env.production
,.env.development
in Flutter app.
Then add some code beforedotenv.load
can do environments separation:
// main.dart...voidmain()async{constenvironment=String.fromEnvironment('ENV',defaultValue:'development');awaitdotenv.load(fileName:'.env.$environment');...
Then run command likeflutter build web --dart-define ENV=production
to spin it up.
But, here is a pitfall. In my practical learning, a file starts with dot.
could cause 404 issue:
So, according tothis suggestion, we'd better use "dot" instead of ".".
1. Rename.env.production
,.env.development
files todotenv.production
,dotenv.development
2. Update assets settings
3. Updatedotenv.load
logic
// main.dart...voidmain()async{constenvironment=String.fromEnvironment('ENV',defaultValue:'development');awaitdotenv.load(fileName:'dotenv.$environment');...
4. Update.gitignore
file
5. After building, delete unnecessarydotenv.*
files inbuild > web > assets
In this case,dotenv.development
is not necessary, so delete it to prevent potential sensitive data leaking.
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse