I've been using flutter_dotenv to load environment variables saved in .env
throughout the application and it's worked just fine to date. As I'm trying to write tests though, I cannot seem to access these from a test file.
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
setUp(() async {
await DotEnv().load();
});
test('Incorrect password should be rejected', () {
var password = DotEnv().env['PASSWORD'];
// stuff
});
}
Result of running the test:
Shell: [flutter_dotenv] Load failed: file not found
Shell: [flutter_dotenv] No env values found. Make sure you have called DotEnv.load()
It just can't seem to find the .env
file. I even made a copy of .env
in the test directory but it didn't recognise that either.
I've tried using Platform.environment instead of flutter_dotenv to access the variable, but that didn't work either, returning null.
Apologies if I'm being silly here, it's my first time writing Flutter tests, but would appreciate advice.
Update:
This is what my pubspec.yaml
looks like:
name: //name
description: //description
version: 1.0.0+3
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.2
font_awesome_flutter: ^8.5.0
flutter_youtube: ^2.0.0
http: ^0.12.0+4
flutter_dotenv: ^2.1.0
google_fonts: ^0.3.7
photo_view: ^0.9.2
flutter_page_transition: ^0.1.0
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: true
assets:
- images/
- .env
pubspec.yaml
? The flutter_dotenv pub page says that the.env
file needs to be added as an asset. – Ariella