java-james / flutter_dotenv

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

DOES NOT WORK #12

Closed qcuong96 closed 3 years ago

qcuong96 commented 3 years ago

I try to use this lib by add in the main function and print to test it. However the result is empty even I define something in .env file This is the main.dart

Future main() async {
  await DotEnv().load(); // already try load('.env')
  print(DotEnv().env); // result is {}
  runApp(MaterialApp(
    debugShowCheckedModeBanner: false,
    title: 'EPOWER MOBILE APP',
    home: FlaskScreen(),
  ));
}

This is the .env file

BASE_URI=test.com.vn
java-james commented 3 years ago

Hi thanks for creating the issue. Have you defined .env as an asset to bundle in the pubspec?

Add the .env file to your assets bundle in pubspec.yaml

  assets:
    - .env

What version of flutter are you using?

qcuong96 commented 3 years ago

Hi thanks for creating the issue. Have you defined .env as an asset to bundle in the pubspec?

Add the .env file to your assets bundle in pubspec.yaml

  assets:
    - .env

What version of flutter are you using?

version: 1.0.0+1

environment: sdk: ">=2.7.0 <3.0.0"

dependencies: http: ^0.12.2 flutter: sdk: flutter

Already added .env

japatel225 commented 3 years ago

Facing the same issue.

japatel225 commented 3 years ago

Hi again, it is working perfectly fine. My mistake was I had created the .env file in the lib directory. Instead it needs to be outside of lib in the root of the project. @qcuong96, verify that you are not making the same mistake.

ug2454 commented 3 years ago

Hi again, it is working perfectly fine. My mistake was I had created the .env file in the lib directory. Instead it needs to be outside of lib in the root of the project. @qcuong96, verify that you are not making the same mistake.

I have tried outside the lib, in the root. still its not working

hazzo commented 3 years ago

For the record (don't know why) but with version 1.20 of Flutter loaded assets can't be in the root dir and must be inside a folder, for example assets folder. I was a experiencing a similar issue and moving the dotenv file inside the assets folder make the trick for me. Also remember that now the load() method should have the path too. Ex.: await DotEnv().load('assets/.env');

Hope it helps

java-james commented 3 years ago

Thanks for this @hazzo I will try this out and confirm the issue then update the usage docs accordingly

GusRodrigues86 commented 3 years ago

For the record (don't know why) but with version 1.20 of Flutter loaded assets can't be in the root dir and must be inside a folder, for example assets folder. I was a experiencing a similar issue and moving the dotenv file inside de de assets folder make the trick for me. Also remember that now the load() method should have the path too. Ex.: await DotEnv().load('assets/.env');

Hope it helps

Running on 1.22 on root folder whatsoever

DoubleBridges commented 3 years ago

I'm having the same issue. I've tried having .env in root as well as an assets folder (with the appropriate changes to pubspec.yaml and the load method). It also gives the same empty object/map if I don't have an .env file at all, though. I did make sure to stop and rebuild between all changes, not just hot reload. Is there anything cached in the build files that could be causing it? I'm very new to flutter and gradle.

Flutter 1.20.3 • channel stable • https://github.com/flutter/flutter.git Framework • revision 216dee60c0 (6 days ago) • 2020-09-01 12:24:47 -0700 Engine • revision d1bc06f032 Tools • Dart 2.9.2

arualana commented 3 years ago

To me is the same as @hazzo , only works with .env in assets folder, would like to have in the root. Flutter version 1.22.0

Must4che commented 3 years ago

It works fine guys. I guess that you're not putting all pieces together correctly.

First of all I would guess that you're misplacing assets in the pubspec.yaml. It has to go like flutter: > assets: > - .env into structure. Then it doesn't matter if you call DotEnv().load('.env') or DotEnv().load()

Both pubspec.yaml and lib/main.dart has to be in sync. Assets has to be set and placed correctly.

azubuikestanley commented 3 years ago

For the record (don't know why) but with version 1.20 of Flutter loaded assets can't be in the root dir and must be inside a folder, for example assets folder. I was a experiencing a similar issue and moving the dotenv file inside de de assets folder make the trick for me. Also remember that now the load() method should have the path too. Ex.: await DotEnv().load('assets/.env');

Hope it helps

Had the same issue, and this worked for me

java-james commented 3 years ago

Have verified working in root on latest flutter. Thanks for the reply @Must4che. Suggest reading his comment above if you are experiencing this issue.

java-james commented 3 years ago

have clarified some instructions in 3.0.0. Please reopen if this is still an issue in the latest release.