Skip to content

Commit b536c85

Browse files
authored
fix: typed getters without fallback silently fail in release (#142)
1 parent 565ac56 commit b536c85

5 files changed

Lines changed: 60 additions & 27 deletions

File tree

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ Release notes are available on [github][notes].
88
[pub-semver-readme]: https://pub.dartlang.org/packages/pub_semver
99
[notes]: https://github.com/java-james/flutter_dotenv/releases
1010

11+
# 6.0.1
12+
13+
- [fix] Replace `assert()` with explicit `if`/`throw` in `getInt()`, `getDouble()`, and `getBool()` so null-safety checks are enforced in release builds (fixes #136)
14+
- Error messages now include the variable name for easier debugging
15+
16+
### Note on release-build behavior change
17+
In **debug mode**, behavior is unchanged — `AssertionError` was thrown before, `AssertionError` is thrown now.
18+
19+
In **release mode**, calling `getInt()`, `getDouble()`, or `getBool()` with a missing variable and no fallback previously threw a `TypeError` (from the null-check operator `!`) because `assert()` was stripped. It now correctly throws `AssertionError` with a descriptive message. If your release-mode code catches `on TypeError` around these methods, update it to catch `on AssertionError` (or `on Error`) instead.
20+
1121
# 6.0.0
1222

1323
- [feat] Allow passing in override .env files on init

example/pubspec.lock

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ packages:
2121
dependency: transitive
2222
description:
2323
name: characters
24-
sha256: f71061c654a3380576a52b451dd5532377954cf9dbd272a78fc8479606670803
24+
sha256: faf38497bda5ead2a8c7615f4f7939df04333478bf32e4173fcb06d428b5716b
2525
url: "https://pub.dev"
2626
source: hosted
27-
version: "1.4.0"
27+
version: "1.4.1"
2828
clock:
2929
dependency: transitive
3030
description:
@@ -118,26 +118,26 @@ packages:
118118
dependency: transitive
119119
description:
120120
name: matcher
121-
sha256: dc58c723c3c24bf8d3e2d3ad3f2f9d7bd9cf43ec6feaa64181775e60190153f2
121+
sha256: dc0b7dc7651697ea4ff3e69ef44b0407ea32c487a39fff6a4004fa585e901861
122122
url: "https://pub.dev"
123123
source: hosted
124-
version: "0.12.17"
124+
version: "0.12.19"
125125
material_color_utilities:
126126
dependency: transitive
127127
description:
128128
name: material_color_utilities
129-
sha256: f7142bb1154231d7ea5f96bc7bde4bda2a0945d2806bb11670e30b850d56bdec
129+
sha256: "9c337007e82b1889149c82ed242ed1cb24a66044e30979c44912381e9be4c48b"
130130
url: "https://pub.dev"
131131
source: hosted
132-
version: "0.11.1"
132+
version: "0.13.0"
133133
meta:
134134
dependency: transitive
135135
description:
136136
name: meta
137-
sha256: e3641ec5d63ebf0d9b41bd43201a66e3fc79a65db5f61fc181f04cd27aab950c
137+
sha256: "23f08335362185a5ea2ad3a4e597f1375e78bce8a040df5c600c8d3552ef2394"
138138
url: "https://pub.dev"
139139
source: hosted
140-
version: "1.16.0"
140+
version: "1.17.0"
141141
path:
142142
dependency: transitive
143143
description:
@@ -195,10 +195,10 @@ packages:
195195
dependency: transitive
196196
description:
197197
name: test_api
198-
sha256: "522f00f556e73044315fa4585ec3270f1808a4b186c936e612cab0b565ff1e00"
198+
sha256: "8161c84903fd860b26bfdefb7963b3f0b68fee7adea0f59ef805ecca346f0c7a"
199199
url: "https://pub.dev"
200200
source: hosted
201-
version: "0.7.6"
201+
version: "0.7.10"
202202
vector_math:
203203
dependency: transitive
204204
description:

lib/src/dotenv.dart

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ class DotEnv {
6666
/// exists but can not be parsed as an [int].
6767
int getInt(String name, {int? fallback}) {
6868
final value = maybeGet(name);
69-
assert(value != null || fallback != null,
70-
'A non-null fallback is required for missing entries');
69+
if (value == null && fallback == null) {
70+
throw AssertionError(
71+
'$name variable not found. A non-null fallback is required for missing entries');
72+
}
7173
return value != null ? int.parse(value) : fallback!;
7274
}
7375

@@ -80,8 +82,10 @@ class DotEnv {
8082
/// exists but can not be parsed as a [double].
8183
double getDouble(String name, {double? fallback}) {
8284
final value = maybeGet(name);
83-
assert(value != null || fallback != null,
84-
'A non-null fallback is required for missing entries');
85+
if (value == null && fallback == null) {
86+
throw AssertionError(
87+
'$name variable not found. A non-null fallback is required for missing entries');
88+
}
8589
return value != null ? double.parse(value) : fallback!;
8690
}
8791

@@ -94,8 +98,10 @@ class DotEnv {
9498
/// exists but can not be parsed as a [bool].
9599
bool getBool(String name, {bool? fallback}) {
96100
final value = maybeGet(name);
97-
assert(value != null || fallback != null,
98-
'A non-null fallback is required for missing entries');
101+
if (value == null && fallback == null) {
102+
throw AssertionError(
103+
'$name variable not found. A non-null fallback is required for missing entries');
104+
}
99105
if (value != null) {
100106
if (['true', '1'].contains(value.toLowerCase())) {
101107
return true;

pubspec.lock

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ packages:
2121
dependency: transitive
2222
description:
2323
name: characters
24-
sha256: f71061c654a3380576a52b451dd5532377954cf9dbd272a78fc8479606670803
24+
sha256: faf38497bda5ead2a8c7615f4f7939df04333478bf32e4173fcb06d428b5716b
2525
url: "https://pub.dev"
2626
source: hosted
27-
version: "1.4.0"
27+
version: "1.4.1"
2828
clock:
2929
dependency: transitive
3030
description:
@@ -103,26 +103,26 @@ packages:
103103
dependency: transitive
104104
description:
105105
name: matcher
106-
sha256: dc58c723c3c24bf8d3e2d3ad3f2f9d7bd9cf43ec6feaa64181775e60190153f2
106+
sha256: dc0b7dc7651697ea4ff3e69ef44b0407ea32c487a39fff6a4004fa585e901861
107107
url: "https://pub.dev"
108108
source: hosted
109-
version: "0.12.17"
109+
version: "0.12.19"
110110
material_color_utilities:
111111
dependency: transitive
112112
description:
113113
name: material_color_utilities
114-
sha256: f7142bb1154231d7ea5f96bc7bde4bda2a0945d2806bb11670e30b850d56bdec
114+
sha256: "9c337007e82b1889149c82ed242ed1cb24a66044e30979c44912381e9be4c48b"
115115
url: "https://pub.dev"
116116
source: hosted
117-
version: "0.11.1"
117+
version: "0.13.0"
118118
meta:
119119
dependency: transitive
120120
description:
121121
name: meta
122-
sha256: e3641ec5d63ebf0d9b41bd43201a66e3fc79a65db5f61fc181f04cd27aab950c
122+
sha256: "23f08335362185a5ea2ad3a4e597f1375e78bce8a040df5c600c8d3552ef2394"
123123
url: "https://pub.dev"
124124
source: hosted
125-
version: "1.16.0"
125+
version: "1.17.0"
126126
path:
127127
dependency: transitive
128128
description:
@@ -180,10 +180,10 @@ packages:
180180
dependency: transitive
181181
description:
182182
name: test_api
183-
sha256: "522f00f556e73044315fa4585ec3270f1808a4b186c936e612cab0b565ff1e00"
183+
sha256: "8161c84903fd860b26bfdefb7963b3f0b68fee7adea0f59ef805ecca346f0c7a"
184184
url: "https://pub.dev"
185185
source: hosted
186-
version: "0.7.6"
186+
version: "0.7.10"
187187
vector_math:
188188
dependency: transitive
189189
description:
@@ -201,5 +201,5 @@ packages:
201201
source: hosted
202202
version: "15.0.2"
203203
sdks:
204-
dart: ">=3.8.0 <4.0.0"
204+
dart: ">=3.9.0-0 <4.0.0"
205205
flutter: ">=3.18.0-18.0.pre.54"

test/dotenv_test.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,5 +132,22 @@ void main() {
132132
expect(dotenv.getBool('COMMENTS', fallback: false), false);
133133
expect(() => dotenv.getBool('FOO'), throwsFormatException);
134134
});
135+
test('typed getter errors include variable name', () {
136+
expect(
137+
() => dotenv.getInt('MISSING_VAR'),
138+
throwsA(predicate((e) =>
139+
e is AssertionError &&
140+
e.message.toString().contains('MISSING_VAR'))));
141+
expect(
142+
() => dotenv.getDouble('MISSING_VAR'),
143+
throwsA(predicate((e) =>
144+
e is AssertionError &&
145+
e.message.toString().contains('MISSING_VAR'))));
146+
expect(
147+
() => dotenv.getBool('MISSING_VAR'),
148+
throwsA(predicate((e) =>
149+
e is AssertionError &&
150+
e.message.toString().contains('MISSING_VAR'))));
151+
});
135152
});
136153
}

0 commit comments

Comments
 (0)