Skip to content

Commit 83bcbd8

Browse files
committed
chore: update readme
1 parent 94f3922 commit 83bcbd8

1 file changed

Lines changed: 16 additions & 14 deletions

File tree

README.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,17 @@ After loading, access values anywhere you import the package:
4545
```dart
4646
import 'package:flutter_dotenv/flutter_dotenv.dart';
4747
48-
String apiUrl = dotenv.get('API_URL');
49-
int retries = dotenv.getInt('MAX_RETRIES', fallback: 1);
50-
bool debug = dotenv.getBool('DEBUG', fallback: false);
48+
// 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);
5152
```
5253

5354
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`.
5455

5556
You do not need to call `WidgetsFlutterBinding.ensureInitialized()` — the library handles this internally when loading assets.
5657

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.
5859
5960
## Security
6061

@@ -121,16 +122,16 @@ await dotenv.load(
121122
| Parameter | Type | Default | Description |
122123
|-----------|------|---------|-------------|
123124
| `fileName` | `String` | `'.env'` | Asset path of the env file (must match `pubspec.yaml`). |
124-
| `overrideWithFiles` | `List<String>` | `[]` | Additional env asset files. |
125-
| `mergeWith` | `Map<String, String>` | `{}` | Key-value pairs to merge. |
125+
| `overrideWithFiles` | `List<String>` | `[]` | Additional env asset files that override the base file. |
126+
| `mergeWith` | `Map<String, String>` | `{}` | Programmatic key-value pairs (highest precedence). |
126127
| `isOptional` | `bool` | `false` | When `true`, missing or empty files don't throw. |
127128
| `parser` | `Parser` | `Parser()` | Custom parser instance. |
128129

129130
When `isOptional` is `true`, `FileNotFoundError` and `EmptyEnvFileError` are suppressed.
130131

131132
### `loadFromString()`
132133

133-
Load variables from a string — useful for tests or computed configuration:
134+
Load variables from a string — useful for tests or in-memory configuration:
134135

135136
```dart
136137
dotenv.loadFromString(envString: 'FOO=bar\nBAZ=qux');
@@ -139,8 +140,8 @@ dotenv.loadFromString(envString: 'FOO=bar\nBAZ=qux');
139140
| Parameter | Type | Default | Description |
140141
|-----------|------|---------|-------------|
141142
| `envString` | `String` | `''` | The env-formatted string to parse. |
142-
| `overrideWith` | `List<String>` | `[]` | Additional env strings whose values take precedence. |
143-
| `mergeWith` | `Map<String, String>` | `{}` | Key-value pairs to merge. |
143+
| `overrideWith` | `List<String>` | `[]` | Additional env strings that override the base string. |
144+
| `mergeWith` | `Map<String, String>` | `{}` | Programmatic key-value pairs (highest precedence). |
144145
| `isOptional` | `bool` | `false` | When `true`, an empty string doesn't throw. |
145146
| `parser` | `Parser` | `Parser()` | Custom parser instance. |
146147

@@ -162,16 +163,16 @@ Both `load()` and `loadFromString()` call `clean()` first, replacing any previou
162163

163164
| Member | Description |
164165
|--------|-------------|
165-
| `dotenv.isInitialized` | `true` after a successful `load()` or `loadFromString()`. |
166+
| `dotenv.isInitialized` | `true` after `load()` or `loadFromString()` completes. |
166167
| `dotenv.clean()` | Clears all loaded variables and resets `isInitialized` to `false`. |
167168

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()`.
169170

170171
### Errors
171172

172173
| Error | Thrown when |
173174
|-------|------------|
174-
| `NotInitializedError` | Accessing `dotenv.env` before loading. |
175+
| `NotInitializedError` | Accessing values before calling `load()` or `loadFromString()`. |
175176
| `FileNotFoundError` | The env file is not in the asset bundle (`load()` only). |
176177
| `EmptyEnvFileError` | The env file or string is empty. |
177178
| `AssertionError` | A required variable is missing and no fallback was provided. |
@@ -202,7 +203,7 @@ export EXPORTED=hello
202203

203204
**Parsing rules:**
204205
- Lines without `=` are ignored.
205-
- `#` starts a comment unless inside quotes.
206+
- `#` starts a comment when it appears outside quoted values.
206207
- Double-quoted values expand `\n` to newlines and interpolate `$VAR` / `${VAR}`.
207208
- Single-quoted values do not interpolate variables. Escaped single quotes (`\'`) are unescaped.
208209
- Undefined variables interpolate to an empty string.
@@ -275,7 +276,8 @@ CLIENT_URL=https://$CLIENT_ID.dev.example.com
275276
|---------|-------|----------|
276277
| `FileNotFoundError` | The file isn't registered as an asset. | Add the exact path to `flutter: assets:` in `pubspec.yaml` and restart the app. |
277278
| `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. |
279281
| `dotenv.env['NAME']` returns `null` | Key mismatch or the file wasn't loaded. | Check for typos. Verify `load()` completed successfully. |
280282
| `mergeWith` value not overridden by `.env` | `mergeWith` has highest precedence. | Move the value into an override file, or stop passing it via `mergeWith`. |
281283
| 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

Comments
 (0)