java-james / flutter_dotenv

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

Instance of 'NotInitializedError' in Flutter integration testing #42

Closed SebghatYusuf closed 2 years ago

SebghatYusuf commented 2 years ago

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 :)

ngxingyu commented 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.

SebghatYusuf commented 2 years ago

@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.

SebghatYusuf commented 2 years ago

Okay @ngxingyu So, I need to move .env file to assets folder, right?

ngxingyu commented 2 years ago

Uh sorry ignore that last message. Just to confirm did you call TestWidgetsFlutterBinding.ensureInitialized(); in your test file?

SebghatYusuf commented 2 years ago

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();
  });
}
SebghatYusuf commented 2 years ago

Ohhh sorry, Let me just add TestWidgetsFlutterBinding.ensureInitialized(); but is it necessary when I have IntegrationTestWidgetsFlutterBinding.ensureInitialized(); ?

ngxingyu commented 2 years ago

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.

SebghatYusuf commented 2 years ago

@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.

ngxingyu commented 2 years ago

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());.

SebghatYusuf commented 2 years ago

@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.

SebghatYusuf commented 2 years ago

ANY UPDATES??

ngxingyu commented 2 years ago

Did you try running TestWidgetsFlutterBinding.ensureInitialized(); before await dotenv.load('.env')?

SebghatYusuf commented 2 years ago

@ngxingyu Thank you, finally it worked.

ziqq commented 1 year ago

@ngxingyu Thank you, it worked.

SimplicitySpace commented 3 months ago

Thanks, I found this helpful