@@ -86,12 +86,27 @@ class LocalPersist {
86
86
/// Make it an empty string to remove it.
87
87
static String defaultDbSubDir = "db" ;
88
88
89
+ /// If running from Flutter, the default base directory is the application's documents dir.
90
+ /// If running from tests (detected by the `LocalFileSystem` not being present),
91
+ /// it will use the system's temp directory.
92
+ ///
93
+ /// You can change this variable to globally change the directory:
94
+ /// ```
95
+ /// // Will use the application's cache directory.
96
+ /// findDirectoryToUse = LocalPersist.findAppCacheDir;
97
+ ///
98
+ /// // Will use the application's downloads directory.
99
+ /// findDirectoryToUse = LocalPersist.findAppDownloadsDir;
100
+ /// ```
101
+ ///
102
+ static Future <void > Function () findBaseDirectory = LocalPersist .findAppDocDir;
103
+
89
104
/// The default is adding a ".db" termination to the file name.
90
105
/// This is not final, so you can change it.
91
106
static String defaultTermination = ".db" ;
92
107
93
- static Directory ? get appDocDir => _appDocDir ;
94
- static Directory ? _appDocDir ;
108
+ static Directory ? get appDocDir => _baseDirectory ;
109
+ static Directory ? _baseDirectory ;
95
110
96
111
// In a json sequence, each object may have at most 65.536 bytes.
97
112
// Note this refers to a single json object, not to the total json sequence file,
@@ -297,7 +312,7 @@ class LocalPersist {
297
312
if (_file != null )
298
313
return _file! ;
299
314
else {
300
- if (_appDocDir == null ) await findAppDocDir ();
315
+ if (_baseDirectory == null ) await findBaseDirectory ();
301
316
String pathNameStr = pathName (
302
317
dbName,
303
318
dbSubDir: dbSubDir,
@@ -319,7 +334,7 @@ class LocalPersist {
319
334
List <String >? subDirs,
320
335
}) {
321
336
return p.joinAll ([
322
- LocalPersist ._appDocDir ! .path,
337
+ LocalPersist ._baseDirectory ! .path,
323
338
dbSubDir ?? LocalPersist .defaultDbSubDir,
324
339
if (subDirs != null ) ...subDirs,
325
340
"$dbName ${LocalPersist .defaultTermination }"
@@ -332,16 +347,46 @@ class LocalPersist {
332
347
/// If running from Flutter, this will get the application's documents directory.
333
348
/// If running from tests, it will use the system's temp directory.
334
349
static Future <void > findAppDocDir () async {
335
- if (_appDocDir != null ) return ;
350
+ if (_baseDirectory != null ) return ;
351
+
352
+ if (_fileSystem == const LocalFileSystem ()) {
353
+ try {
354
+ _baseDirectory = await getApplicationDocumentsDirectory ();
355
+ } on MissingPluginException catch (_) {
356
+ _baseDirectory = const LocalFileSystem ().systemTempDirectory;
357
+ }
358
+ } else
359
+ _baseDirectory = _fileSystem.systemTempDirectory;
360
+ }
361
+
362
+ /// If running from Flutter, this will get the application's cache directory.
363
+ /// If running from tests, it will use the system's temp directory.
364
+ static Future <void > findAppCacheDir () async {
365
+ if (_baseDirectory != null ) return ;
366
+
367
+ if (_fileSystem == const LocalFileSystem ()) {
368
+ try {
369
+ _baseDirectory = await getApplicationCacheDirectory ();
370
+ } on MissingPluginException catch (_) {
371
+ _baseDirectory = const LocalFileSystem ().systemTempDirectory;
372
+ }
373
+ } else
374
+ _baseDirectory = _fileSystem.systemTempDirectory;
375
+ }
376
+
377
+ /// If running from Flutter, this will get the application's downloads directory.
378
+ /// If running from tests, it will use the system's temp directory.
379
+ static Future <void > findAppDownloadsDir () async {
380
+ if (_baseDirectory != null ) return ;
336
381
337
382
if (_fileSystem == const LocalFileSystem ()) {
338
383
try {
339
- _appDocDir = await getApplicationDocumentsDirectory ();
384
+ _baseDirectory = await getDownloadsDirectory ();
340
385
} on MissingPluginException catch (_) {
341
- _appDocDir = const LocalFileSystem ().systemTempDirectory;
386
+ _baseDirectory = const LocalFileSystem ().systemTempDirectory;
342
387
}
343
388
} else
344
- _appDocDir = _fileSystem.systemTempDirectory;
389
+ _baseDirectory = _fileSystem.systemTempDirectory;
345
390
}
346
391
347
392
static Uint8List encode (List <Object > simpleObjs) {
0 commit comments