Closed SebghatYusuf closed 2 years ago
Can you share a sample of your code? The error should not happen if you called something like await dotenv.load(fileName: "assets/.env");
and set the path in pubspec.yaml, assuming the .env file in the assets folder.
@ngxingyu sure, I will add code example, I have the .env file in my project directory not in assets folder but I'm adding it in my pubspec.yaml
file like this:
assets:
- images/
- .env
and I'm loading the dotenv file in my main function as below:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
await FirebaseCrashlytics.instance.setCrashlyticsCollectionEnabled(true);
// FirebaseCrashlytics.instance.crash();
await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
await dotenv.load(fileName: '.env');
runApp(MyApp());
}
it's working fine when I'm running in debug, release mode but it's just not working when trying to run Integration tests.
Okay @ngxingyu So, I need to move .env file to assets folder, right?
Uh sorry ignore that last message. Just to confirm did you call TestWidgetsFlutterBinding.ensureInitialized();
in your test file?
Uh sorry ignore that last message. Just to confirm did you call
TestWidgetsFlutterBinding.ensureInitialized();
in your test file?
Yeah, I did:
main() async {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
testWidgets("Testing app", (WidgetTester tester) async {
await tester.pumpWidget(MyApp());
await tester.pumpAndSettle();
await testVin(tester);
await tester.pump();
});
}
Ohhh sorry, Let me just add TestWidgetsFlutterBinding.ensureInitialized();
but is it necessary when I have IntegrationTestWidgetsFlutterBinding.ensureInitialized();
?
I haven't used the integration test package before, but IntegrationTestWidgetsFlutterBinding.ensureInitialized();
should be sufficient. Did you call await dotenv.load(fileName: '.env');
before your tests? Essentially the load method runs rootBundle.loadString('.env')
and populates the dotenv object.
@ngxingyu Actually I didn't, I thought maybe because I'm calling it inside MyApp()
widget, that would be enough.
But I've tried that as well I've called await dotenv.load(fileName: '.env');
before tests and still I wasn't able to read the dotenv file I've tried relative path as well, await dotenv.load(fileName:'../.env');
because the test file is inside intgration_test folder, but it didn't worked.
I think NotInitializedError
occurs when dotenv.env
is called before the dotenv.load
is called properly, and if the dotenv.load
is called but fails it will throw FileNotFoundError
or EmptyEnvFileError
, so perhaps can try double checking if the load is called before .env.
Alternatively, a possible workaround can be to directly import the .env file and do dotenv.testLoad(fileInput: File('.env').readAsStringSync());
.
@ngxingyu I just added await dotenv.load('.env')
before IntegrationTestWidgetsFlutterBinding.ensureInitialized();
and now I don't get the NotInitializedError
but still the value i'm retrieving form dotenv is null
.
ANY UPDATES??
Did you try running TestWidgetsFlutterBinding.ensureInitialized(); before await dotenv.load('.env')?
@ngxingyu Thank you, finally it worked.
@ngxingyu Thank you, it worked.
Thanks, I found this helpful
When I'm trying to run integration testing, it's not working and I see this in a try catch block as error. Please let me know how I can handle this, any help would be highly appreciated :)