Skip to content

Commit f368088

Browse files
authored
chore: format with dart format (#113)
1 parent 7ded2fa commit f368088

File tree

5 files changed

+62
-40
lines changed

5 files changed

+62
-40
lines changed

example/lib/main.dart

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,32 @@ class MyApp extends StatelessWidget {
1919
return MaterialApp(
2020
title: 'flutter_dotenv Demo',
2121
home: Scaffold(
22-
appBar: AppBar(
23-
title: const Text('Dotenv Demo'),
24-
),
25-
body: SingleChildScrollView(
26-
child: FutureBuilder<String>(
27-
future: rootBundle.loadString('assets/.env'),
28-
initialData: '',
29-
builder: (context, snapshot) => Container(
30-
padding: const EdgeInsets.all(50),
31-
child: Column(
32-
children: [
33-
Text(
34-
'Env map: ${dotenv.env.toString()}',
35-
),
36-
const Divider(thickness: 5),
37-
const Text('Original'),
38-
const Divider(),
39-
Text(snapshot.data ?? ''),
40-
Text(dotenv.get('MISSING', fallback: 'Default fallback value')),
41-
],
42-
),
22+
appBar: AppBar(
23+
title: const Text('Dotenv Demo'),
24+
),
25+
body: SingleChildScrollView(
26+
child: FutureBuilder<String>(
27+
future: rootBundle.loadString('assets/.env'),
28+
initialData: '',
29+
builder: (context, snapshot) => Container(
30+
padding: const EdgeInsets.all(50),
31+
child: Column(
32+
children: [
33+
Text(
34+
'Env map: ${dotenv.env.toString()}',
35+
),
36+
const Divider(thickness: 5),
37+
const Text('Original'),
38+
const Divider(),
39+
Text(snapshot.data ?? ''),
40+
Text(dotenv.get('MISSING',
41+
fallback: 'Default fallback value')),
42+
],
4343
),
4444
),
4545
),
4646
),
47+
),
4748
);
4849
}
49-
}
50+
}

lib/src/dotenv.dart

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ class DotEnv {
5050

5151
String get(String name, {String? fallback}) {
5252
final value = maybeGet(name, fallback: fallback);
53-
if(value == null){
54-
throw AssertionError('$name variable not found. A non-null fallback is required for missing entries');
53+
if (value == null) {
54+
throw AssertionError(
55+
'$name variable not found. A non-null fallback is required for missing entries');
5556
}
5657
return value;
5758
}
@@ -65,7 +66,8 @@ class DotEnv {
6566
/// exists but can not be parsed as an [int].
6667
int getInt(String name, {int? fallback}) {
6768
final value = maybeGet(name);
68-
assert(value != null || fallback != null, 'A non-null fallback is required for missing entries');
69+
assert(value != null || fallback != null,
70+
'A non-null fallback is required for missing entries');
6971
return value != null ? int.parse(value) : fallback!;
7072
}
7173

@@ -78,7 +80,8 @@ class DotEnv {
7880
/// exists but can not be parsed as a [double].
7981
double getDouble(String name, {double? fallback}) {
8082
final value = maybeGet(name);
81-
assert(value != null || fallback != null, 'A non-null fallback is required for missing entries');
83+
assert(value != null || fallback != null,
84+
'A non-null fallback is required for missing entries');
8285
return value != null ? double.parse(value) : fallback!;
8386
}
8487

@@ -91,7 +94,8 @@ class DotEnv {
9194
/// exists but can not be parsed as a [bool].
9295
bool getBool(String name, {bool? fallback}) {
9396
final value = maybeGet(name);
94-
assert(value != null || fallback != null, 'A non-null fallback is required for missing entries');
97+
assert(value != null || fallback != null,
98+
'A non-null fallback is required for missing entries');
9599
if (value != null) {
96100
if (['true', '1'].contains(value.toLowerCase())) {
97101
return true;
@@ -110,7 +114,10 @@ class DotEnv {
110114
/// Loads environment variables from the env file into a map
111115
/// Merge with any entries defined in [mergeWith]
112116
Future<void> load(
113-
{String fileName = '.env',Parser parser = const Parser(),Map<String, String> mergeWith = const {}, bool isOptional = false}) async {
117+
{String fileName = '.env',
118+
Parser parser = const Parser(),
119+
Map<String, String> mergeWith = const {},
120+
bool isOptional = false}) async {
114121
clean();
115122
List<String> linesFromFile;
116123
try {
@@ -123,7 +130,9 @@ class DotEnv {
123130
}
124131
}
125132

126-
final linesFromMergeWith = mergeWith.entries.map((entry) => "${entry.key}=${entry.value}").toList();
133+
final linesFromMergeWith = mergeWith.entries
134+
.map((entry) => "${entry.key}=${entry.value}")
135+
.toList();
127136
final allLines = linesFromMergeWith..addAll(linesFromFile);
128137
final envEntries = parser.parse(allLines);
129138
_envMap.addAll(envEntries);
@@ -136,7 +145,9 @@ class DotEnv {
136145
Map<String, String> mergeWith = const {}}) {
137146
clean();
138147
final linesFromFile = fileInput.split('\n');
139-
final linesFromMergeWith = mergeWith.entries.map((entry) => "${entry.key}=${entry.value}").toList();
148+
final linesFromMergeWith = mergeWith.entries
149+
.map((entry) => "${entry.key}=${entry.value}")
150+
.toList();
140151
final allLines = linesFromMergeWith..addAll(linesFromFile);
141152
final envEntries = parser.parse(allLines);
142153
_envMap.addAll(envEntries);
@@ -146,7 +157,8 @@ class DotEnv {
146157
/// True if all supplied variables have nonempty value; false otherwise.
147158
/// Differs from [containsKey](dart:core) by excluding null values.
148159
/// Note [load] should be called first.
149-
bool isEveryDefined(Iterable<String> vars) => vars.every((k) => _envMap[k]?.isNotEmpty ?? false);
160+
bool isEveryDefined(Iterable<String> vars) =>
161+
vars.every((k) => _envMap[k]?.isNotEmpty ?? false);
150162

151163
Future<List<String>> _getEntriesFromFile(String filename) async {
152164
try {

lib/src/errors.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,3 @@ class NotInitializedError extends Error {}
33
class FileNotFoundError extends Error {}
44

55
class EmptyEnvFileError extends Error {}
6-
7-

lib/src/parser.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ class Parser {
2424
}
2525

2626
/// Parses a single line into a key-value pair.
27-
Map<String, String> parseOne(String line, {Map<String, String> env = const {}}) {
27+
Map<String, String> parseOne(String line,
28+
{Map<String, String> env = const {}}) {
2829
var stripped = strip(line);
2930
if (!_isValid(stripped)) return {};
3031

@@ -48,7 +49,8 @@ class Parser {
4849
}
4950

5051
/// Substitutes $bash_vars in [val] with values from [env].
51-
String interpolate(String val, Map<String, String?> env) => val.replaceAllMapped(_bashVar, (m) {
52+
String interpolate(String val, Map<String, String?> env) =>
53+
val.replaceAllMapped(_bashVar, (m) {
5254
if ((m.group(1) ?? "") == "\\") {
5355
return m.input.substring(m.start, m.end);
5456
} else {
@@ -84,5 +86,6 @@ class Parser {
8486
bool _isValid(String s) => s.isNotEmpty && s.contains('=');
8587

8688
/// [ null ] is a valid value in a Dart map, but the env var representation is empty string, not the string 'null'
87-
bool _has(Map<String, String?> map, String key) => map.containsKey(key) && map[key] != null;
89+
bool _has(Map<String, String?> map, String key) =>
90+
map.containsKey(key) && map[key] != null;
8891
}

test/dotenv_test.dart

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ void main() {
66
group('dotenv', () {
77
setUp(() {
88
print(Directory.current.toString());
9-
dotenv.testLoad(fileInput: File('test/.env').readAsStringSync()); // mergeWith: Platform.environment
9+
dotenv.testLoad(
10+
fileInput: File('test/.env')
11+
.readAsStringSync()); // mergeWith: Platform.environment
1012
});
1113
test('when .env is loaded we should be able to get the .env variables', () {
1214
expect(dotenv.env['FOO'], 'foo');
@@ -46,15 +48,21 @@ void main() {
4648
expect(dotenv.env['USERNAME'], '[email protected]');
4749
expect(dotenv.env['SPACED_KEY'], 'parsed');
4850
});
49-
test('when getting a vairable that is not in .env, we should get the fallback we defined', () {
51+
test(
52+
'when getting a vairable that is not in .env, we should get the fallback we defined',
53+
() {
5054
expect(dotenv.get('FOO', fallback: 'bar'), 'foo');
5155
expect(dotenv.get('COMMENTS', fallback: 'sample'), 'sample');
5256
expect(dotenv.get('EQUAL_SIGNS', fallback: 'sample'), 'equals==');
5357
});
54-
test('when getting a vairable that is not in .env, we should get an error thrown', () {
58+
test(
59+
'when getting a vairable that is not in .env, we should get an error thrown',
60+
() {
5561
expect(() => dotenv.get('COMMENTS'), throwsAssertionError);
5662
});
57-
test('when getting a vairable using the nullable getter, we should get null if no fallback is defined', () {
63+
test(
64+
'when getting a vairable using the nullable getter, we should get null if no fallback is defined',
65+
() {
5866
expect(dotenv.maybeGet('COMMENTS'), null);
5967
expect(dotenv.maybeGet('COMMENTS', fallback: 'sample'), 'sample');
6068
expect(dotenv.maybeGet('EQUAL_SIGNS', fallback: 'sample'), 'equals==');

0 commit comments

Comments
 (0)