java-james / flutter_dotenv

Loads environment variables from `.env`.
https://pub.dartlang.org/packages/flutter_dotenv
MIT License
209 stars 46 forks source link

mergeWith too limited (cannot merge with itself / cannot overwrite values) #69

Open martin-braun opened 1 year ago

martin-braun commented 1 year ago

I try to load an additional .env when running in a emulator/simulator like so:

dotenv.load(fileName: '.env').then((value) => {
  SafeDevice.isRealDevice.then((isReal) => {
    if (!isReal && (Platform.isAndroid || Platform.isIOS))
    {
      dotenv.load(
        fileName: '.env.' + (Platform.isAndroid ? "emulator" : "simulator"),
        mergeWith: dotenv.env)
      .then((value) => _runApp())
    }
  })
});

Nope, that it not working. It ends up with a map that contains only the keys of .env.emulator / .env.simulator. All keys from .env are lost. Hitting a breakpoint before the 2nd load reveals that the .env file is properly load.

EDIT: Workaround is to run mergeWith: {...dotenv.env}, but maybe this lib should do it by itself. Also it seems the merged map has higher priority, which feels wrong. I have to load the .env.emulator/ .env.simulator first, although the term mergeWith implies that it will be merged into the given map and thus values should be replaced.

Maybe an argument that defines if merged values will be overwritten or not? Last load values should have higher priority, but this would introduce a breaking change. An argument to toggle priority/overwrite would not.