Open jerinho opened 2 years 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.
You probably put the .env file inside of the lib folder. Instead, you should put it into the root directory one layer above.
I have the same problem.
Project structure
.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(),
),
);
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.
Some issue, I fix add this: await dotenv.load(fileName: "config.env");
assets:
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.
I have the same problem.
Project structure
.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(), ), );
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
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):