-
I know this is not dotenv_flutter's fault but rather how rootBundle works in tests, but loading environment variables in an integration test is essential for the test to work like in the real world, and every person on the planet loading the .env file is going to encounter this problem. I have an integration test that loads environment variables. Inside import 'package:user_app/main.dart' as app;
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
group('end-to-end test', () {
testWidgets('reads mailing list', (WidgetTester tester) async {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
// FlutterError (Unable to load asset: .env)
final env = await rootBundle.loadString(".env");
await app.main();
await tester.pumpAndSettle();
});
});
} pubspec.yaml # ...rest of yaml file
flutter:
assets:
- .env The |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
If I had just READ THE DOCUMENTATION, I would have seen the section about testing. https://pub.dev/packages/flutter_dotenv#using-in-tests My test now looks like this, and it works import 'package:user_app/main.dart' as app;
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
group('end-to-end test', () {
testWidgets('reads mailing list', (WidgetTester tester) async {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
// Now I can access environment variables from dotenv.env[]
dotenv.testLoad(fileInput: File("./.env").readAsStringSync());
await app.main();
await tester.pumpAndSettle();
});
});
} |
Beta Was this translation helpful? Give feedback.
If I had just READ THE DOCUMENTATION, I would have seen the section about testing.
https://pub.dev/packages/flutter_dotenv#using-in-tests
My test now looks like this, and it works