From ebdeb3d5e021461d266785752b5f0e23f5e8e0cc Mon Sep 17 00:00:00 2001 From: Ricardo Boss Date: Thu, 2 Mar 2023 18:30:04 +0100 Subject: [PATCH 1/2] Added 'optional' parameter can handle exception if file cannot be found --- lib/src/dotenv.dart | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/src/dotenv.dart b/lib/src/dotenv.dart index cd1e788..1a8175a 100644 --- a/lib/src/dotenv.dart +++ b/lib/src/dotenv.dart @@ -61,9 +61,19 @@ class DotEnv { /// Loads environment variables from the env file into a map /// Merge with any entries defined in [mergeWith] Future load( - {String fileName = '.env',Parser parser = const Parser(),Map mergeWith = const {}}) async { + {String fileName = '.env',Parser parser = const Parser(),Map mergeWith = const {}, bool optional = false}) async { clean(); - final linesFromFile = await _getEntriesFromFile(fileName); + List linesFromFile; + try { + linesFromFile = await _getEntriesFromFile(fileName); + } on FileNotFoundError { + if (optional) { + linesFromFile = []; + } else { + rethrow; + } + } + final linesFromMergeWith = mergeWith.entries.map((entry) => "${entry.key}=${entry.value}").toList(); final allLines = linesFromMergeWith..addAll(linesFromFile); final envEntries = parser.parse(allLines); From fd1723cbfce0b578a8479ef132ac9970e0cdc25e Mon Sep 17 00:00:00 2001 From: James Collins Date: Thu, 1 Jun 2023 15:09:06 +1200 Subject: [PATCH 2/2] chore: rename var --- lib/src/dotenv.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/dotenv.dart b/lib/src/dotenv.dart index 1a8175a..3456784 100644 --- a/lib/src/dotenv.dart +++ b/lib/src/dotenv.dart @@ -61,13 +61,13 @@ class DotEnv { /// Loads environment variables from the env file into a map /// Merge with any entries defined in [mergeWith] Future load( - {String fileName = '.env',Parser parser = const Parser(),Map mergeWith = const {}, bool optional = false}) async { + {String fileName = '.env',Parser parser = const Parser(),Map mergeWith = const {}, bool isOptional = false}) async { clean(); List linesFromFile; try { linesFromFile = await _getEntriesFromFile(fileName); } on FileNotFoundError { - if (optional) { + if (isOptional) { linesFromFile = []; } else { rethrow;