java-james / flutter_dotenv

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

FileNotFoundError #72

Open jerinho opened 1 year ago

jerinho commented 1 year ago

E/flutter (29283): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: Instance of 'FileNotFoundError' E/flutter (29283): #0 DotEnv._getEntriesFromFile (package:flutter_dotenv/src/dotenv.dart:98:7) E/flutter (29283): E/flutter (29283): #1 DotEnv.load (package:flutter_dotenv/src/dotenv.dart:65:27) E/flutter (29283): E/flutter (29283): #2 main (package:menu/main.dart:12:3) E/flutter (29283): E/flutter (29283):

aatmmr commented 1 year ago

@jerinho, have you added the reference to your .env file to the pubspec.yaml? Make sure the reference is located, such as,

flutter:
  [...]

  assets:
  - .env

where assets is in the flutter section.

Otherwise, check if your name is other than .env and if so, that the initialisation specifies the fileName, such as,

await dotenv.load(fileName: ".env.yourName");

I hope that helps.

fleeser commented 1 year ago

You probably put the .env file inside of the lib folder. Instead, you should put it into the root directory one layer above.

bleszerd commented 1 year ago

I have the same problem.

Project structure

image

.env

TWITTER_API_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXX
TWITTER_API_SECRET=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

pubspec.yaml

dependencies:
  flutter:
    sdk: flutter

  #...
  flutter_dotenv: ^5.0.2
  #...

  assets:
    - assets/icons/
    - .env

main.dart

WidgetsFlutterBinding.ensureInitialized();
DartPluginRegistrant.ensureInitialized();

await dotenv.load(
  fileName: ".env",
);

runApp(
  StoreProvider(
    store: appStore,
    child: const MyAppName(),
  ),
);

flutter_build_android_31.log

A curious fact is that I ran the exact same code a few days ago on Linux and it worked, just like when I came to Windows. As much as I believe this did not influence, I thought it best to say this.

KevinVegaRem commented 1 year ago

Some issue, I fix add this: await dotenv.load(fileName: "config.env");

assets:

ferrarafer commented 1 year ago

This issue in general is because the .env file is empty. I wasted a huge amount of time until I realize this. The error should be different or should be no error on empty files.

I think this behavior could be different depending on the operative system. I made my tests on macOS Ventura using flutter web.

gargashwani commented 11 months ago

I have the same problem.

Project structure

image

.env

TWITTER_API_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXX
TWITTER_API_SECRET=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

pubspec.yaml

dependencies:
  flutter:
    sdk: flutter

  #...
  flutter_dotenv: ^5.0.2
  #...

  assets:
    - assets/icons/
    - .env

main.dart

WidgetsFlutterBinding.ensureInitialized();
DartPluginRegistrant.ensureInitialized();

await dotenv.load(
  fileName: ".env",
);

runApp(
  StoreProvider(
    store: appStore,
    child: const MyAppName(),
  ),
);

flutter_build_android_31.log

A curious fact is that I ran the exact same code a few days ago on Linux and it worked, just like when I came to Windows. As much as I believe this did not influence, I thought it best to say this.

Saved Much Time.

Thanks