flutter_dotenv icon indicating copy to clipboard operation
flutter_dotenv copied to clipboard

Unable to use flutter_dontenv with web

Open pbhuvanesh opened this issue 4 years ago • 11 comments

Hello,

`$ 🐓 flutter doctor --verbose [✓] Flutter (Channel stable, 2.0.4, on Linux, locale en_US.UTF-8) • Flutter version 2.0.4 at /home/bhuvanesh/Desktop/flutter • Framework revision b1395592de (3 weeks ago), 2021-04-01 14:25:01 -0700 • Engine revision 2dce47073a • Dart version 2.12.2

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3) • Android SDK at /home/bhuvanesh/Android/Sdk/ • Platform android-30, build-tools 29.0.3 • ANDROID_HOME = /home/bhuvanesh/Android/Sdk/ • Java binary at: /usr/local/android-studio/jre/bin/java • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405) • All Android licenses accepted.

[✓] Chrome - develop for the web • Chrome at google-chrome

[✓] Android Studio (version 3.5) • Android Studio at /usr/local/android-studio • Flutter plugin version 43.0.1 • Dart plugin version 191.8593 • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)

[✓] Connected device (2 available) • SM G950F (mobile) • ce081718555a150201 • android-arm64 • Android 9 (API 28) • Chrome (web) • chrome • web-javascript • Google Chrome 89.0.4389.114

• No issues found! `

when I do flutter run -d chrome I get

`flutter run -d chrome Launching lib/main.dart on Chrome in debug mode... Waiting for connection from debug service on Chrome... 14.1s Debug service listening on ws://127.0.0.1:35647/xS1ZztduJ5I=/ws

💪 Running with sound null safety 💪

🔥 To hot restart changes while running, press "r" or "R". For a more detailed help message, press "h". To quit, press "q". Error: Instance of 'NotInitializedError' at Object.throw_ [as throw] (http://localhost:37417/dart_sdk.js:5031:11) at Object.get env [as env] (http://localhost:37417/packages/flutter_dotenv/src/dotenv.dart.lib.js:138:1 9) at Object.getServerUrl (http://localhost:37417/packages/kaadhal_client/constants/constants.dart.lib .js:60:22) at main (http://localhost:37417/packages/kaadhal_client/main.dart.lib.js:57:122) at main.next () at runBody (http://localhost:37417/dart_sdk.js:37228:34) at Object._async [as async] (http://localhost:37417/dart_sdk.js:37259:7) at main$ (http://localhost:37417/packages/kaadhal_client/main.dart.lib.js:54:18) at main (http://localhost:37417/web_entrypoint.dart.lib.js:47:29) at main.next () at http://localhost:37417/dart_sdk.js:37209:33 at _RootZone.runUnary (http://localhost:37417/dart_sdk.js:37080:59) at _FutureListener.thenAwait.handleValue (http://localhost:37417/dart_sdk.js:32336:29) at handleValueCallback (http://localhost:37417/dart_sdk.js:32863:49) at Function._propagateToListeners (http://localhost:37417/dart_sdk.js:32901:17) at _Future.new.[_completeWithValue] (http://localhost:37417/dart_sdk.js:32749:23) at http://localhost:37417/dart_sdk.js:32000:46 at _RootZone.runUnary (http://localhost:37417/dart_sdk.js:37080:59) at _FutureListener.then.handleValue (http://localhost:37417/dart_sdk.js:32336:29) at handleValueCallback (http://localhost:37417/dart_sdk.js:32863:49) at Function._propagateToListeners (http://localhost:37417/dart_sdk.js:32901:17) at _Future.new.[_completeWithValue] (http://localhost:37417/dart_sdk.js:32749:23) at async._AsyncCallbackEntry.new.callback (http://localhost:37417/dart_sdk.js:32770:35) at Object._microtaskLoop (http://localhost:37417/dart_sdk.js:37332:13) at _startMicrotaskLoop (http://localhost:37417/dart_sdk.js:37338:13) at http://localhost:37417/dart_sdk.js:33109:9

`

My pubspec.yaml dependencies: flutter: sdk: flutter flutter_dotenv: 4.0.0-nullsafety.0 flutter: assets: # - .env - dotenv - assets/images/

Can you please tell me if I am doing something wrong here

I am also using dotenv as file name as opposed to .env

pbhuvanesh avatar Apr 24 '21 04:04 pbhuvanesh

Did you run dotenv.load(fileName: 'dotenv'); before you called dotenv.env?

ngxingyu avatar Apr 25 '21 08:04 ngxingyu

Yes I did my dontenv.load is the 2nd line after EnsureInitialized

On Sun, Apr 25, 2021 at 1:56 PM ngxingyu @.***> wrote:

Did you run dotenv.load(fileName: 'dotenv'); before you called dotenv.env?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/java-james/flutter_dotenv/issues/31#issuecomment-826282148, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACYT3IHT4NCL4T5DOORARH3TKPG3NANCNFSM43PXMGOA .

pbhuvanesh avatar Apr 25 '21 08:04 pbhuvanesh

Did you await the dotenv.load function? The NotInitializedError is only thrown if _isInitialized in dotenv.dart is false, and that is set to true at the end of the dotenv.load() function.

ngxingyu avatar Apr 25 '21 09:04 ngxingyu

void main() async { WidgetsFlutterBinding.ensureInitialized(); initialize(); await DotEnv.load(); HttpOverrides.global = new MyHttpOverrides(); FlavorConfig( flavor: Flavor.DEV, values: FlavorValues(baseUrl: getServerUrl(Flavor.DEV.toString()))); setPathUrlStrategy(); runApp(App()); }

Here is my code. I've awaited the load yet this happens.

pbhuvanesh avatar Apr 26 '21 01:04 pbhuvanesh

I can't tell what the issue is. Perhaps you can try running the example app as a baseline, or try printing DotEnv.env.toString() right after the await DotEnv.load() to test if that works?

ngxingyu avatar Apr 26 '21 10:04 ngxingyu

Slightly late but i suspect there was a call to .env in the initialize() before Dotenv.load so perhaps shifting the dotenv.load up one line would resolve this.

ngxingyu avatar Jul 22 '21 23:07 ngxingyu

Are you trying to use Platform.environment with Flutter Web and Flutter Dot Env? If so then know that Platform.environment does not work with Flutter. Only Mobile and Desktop

awasum avatar Aug 19 '21 13:08 awasum

@pbhuvanesh did you add .env to your pubspec.yaml assets?

assets:
    - .env

This would be if the .env was in your root directory

khari998 avatar Sep 14 '21 11:09 khari998

Is there any fix?

lcsvcn avatar Apr 04 '22 18:04 lcsvcn

Is there any fix?

What I ended up doing is moving to Netlify and storying environment variables on their server. Was able to access them using Netlify functions with the JavaScript process.env syntax.

The only other way that got me half way there was to generate a script for the .env mid deployment so that it was created server side every time there was a new build. However even this way was not ideal from a security pov as keys weren't hidden and I still had issues with my requests made from the client because of it.

The easiest way to go about this for Flutter Web imo is to try and remove environment variables from the client side completely and only store them in the server. This is not an issue for mobile devices as the keys aren't easily exposable in the same capacity.

khari998 avatar Apr 04 '22 19:04 khari998

Today i had a similar situation but on Android app, and only on production environment (PlayStore)... The app was throwing NotInitializedError and after FormatException... After many tries, what work for me was remove the comments with special chars on .env. I was have comments with some characters like "ç" or "ã", because its commom on portuguese language.

cristianms avatar Nov 04 '22 17:11 cristianms