You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Inside a function, widget, or service — not as a top-level initializer
49
+
final apiUrl = dotenv.get('API_URL');
50
+
final retries = dotenv.getInt('MAX_RETRIES', fallback: 1);
51
+
final debug = dotenv.getBool('DEBUG', fallback: false);
51
52
```
52
53
53
54
If your `.env` file is in a subdirectory (e.g. `assets/.env`), pass `fileName: 'assets/.env'` to `load()` and register the same path in `pubspec.yaml`.
54
55
55
56
You do not need to call `WidgetsFlutterBinding.ensureInitialized()` — the library handles this internally when loading assets.
56
57
57
-
> **Tip:** Add `.env` to your `.gitignore` if it contains values you don't want in version control.
58
+
> **Tip:** Add `.env`and any environment-specific variants (e.g. `.env.staging`) to your `.gitignore` if they should not be committed.
58
59
59
60
## Security
60
61
@@ -121,16 +122,16 @@ await dotenv.load(
121
122
| Parameter | Type | Default | Description |
122
123
|-----------|------|---------|-------------|
123
124
|`fileName`|`String`|`'.env'`| Asset path of the env file (must match `pubspec.yaml`). |
@@ -162,16 +163,16 @@ Both `load()` and `loadFromString()` call `clean()` first, replacing any previou
162
163
163
164
| Member | Description |
164
165
|--------|-------------|
165
-
|`dotenv.isInitialized`|`true` after a successful `load()` or `loadFromString()`. |
166
+
|`dotenv.isInitialized`|`true` after `load()` or `loadFromString()` completes. |
166
167
|`dotenv.clean()`| Clears all loaded variables and resets `isInitialized` to `false`. |
167
168
168
-
After calling `clean()`, all read methods (`env`, `get()`, `maybeGet()`, typed getters) will throw `NotInitializedError` until the next `load()` or `loadFromString()`.
169
+
After `clean()`, the instance is uninitialized. Accessing values will throw `NotInitializedError` until the next `load()` or `loadFromString()`.
169
170
170
171
### Errors
171
172
172
173
| Error | Thrown when |
173
174
|-------|------------|
174
-
|`NotInitializedError`| Accessing `dotenv.env` before loading. |
175
+
|`NotInitializedError`| Accessing values before calling `load()` or `loadFromString()`. |
175
176
|`FileNotFoundError`| The env file is not in the asset bundle (`load()` only). |
176
177
|`EmptyEnvFileError`| The env file or string is empty. |
177
178
|`AssertionError`| A required variable is missing and no fallback was provided. |
@@ -202,7 +203,7 @@ export EXPORTED=hello
202
203
203
204
**Parsing rules:**
204
205
- Lines without `=` are ignored.
205
-
-`#` starts a comment unless inside quotes.
206
+
-`#` starts a comment when it appears outside quoted values.
206
207
- Double-quoted values expand `\n` to newlines and interpolate `$VAR` / `${VAR}`.
207
208
- Single-quoted values do not interpolate variables. Escaped single quotes (`\'`) are unescaped.
208
209
- Undefined variables interpolate to an empty string.
|`FileNotFoundError`| The file isn't registered as an asset. | Add the exact path to `flutter: assets:` in `pubspec.yaml` and restart the app. |
277
278
|`EmptyEnvFileError`| The env file or string has no content. | Add at least one `KEY=value` entry, or use `isOptional: true`. |
278
-
|`NotInitializedError`| Accessing `dotenv.env` before loading. | Call `await dotenv.load()` in `main()` before `runApp()`. |
279
+
|`NotInitializedError`| Accessing values before loading. | Call `await dotenv.load()` in `main()` before `runApp()`. |
280
+
|`NotInitializedError` despite `load()` in `main()`| Values read in top-level initializers run before `main()`. | Move reads inside functions, widgets, or callbacks. |
279
281
|`dotenv.env['NAME']` returns `null`| Key mismatch or the file wasn't loaded. | Check for typos. Verify `load()` completed successfully. |
280
282
|`mergeWith` value not overridden by `.env`|`mergeWith` has highest precedence. | Move the value into an override file, or stop passing it via `mergeWith`. |
281
283
| Web deploy: file not found | Web servers may ignore dotfiles. | Rename the file (e.g. `env` or `config.env`) and update `fileName` and `pubspec.yaml`. |
0 commit comments