diff --git a/hive_flutter/CHANGELOG.md b/hive_flutter/CHANGELOG.md index 520f6dbe..91129170 100644 --- a/hive_flutter/CHANGELOG.md +++ b/hive_flutter/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.4.0 + +- `Hive.initFlutter` uses absolute paths as-is without calling `getApplicationDocumentsDirectory` (by [@jheld](https://github.com/jheld) in [#293](https://github.com/IO-Design-Team/hive_ce/pull/293)) + ## 2.3.4 - Adds `hive_ce_flutter.dart` to make the publish action happy diff --git a/hive_flutter/lib/src/hive_extensions.dart b/hive_flutter/lib/src/hive_extensions.dart index d11776db..e6e353ca 100644 --- a/hive_flutter/lib/src/hive_extensions.dart +++ b/hive_flutter/lib/src/hive_extensions.dart @@ -8,13 +8,15 @@ import 'package:hive_ce_flutter/src/wrapper/path.dart' as path_helper; extension HiveX on HiveInterface { /// Initializes Hive with the path from [getApplicationDocumentsDirectory]. /// - /// You can provide a [subDir] where the boxes should be stored. + /// You can provide a [dir] where the boxes should be stored. Relative paths + /// are resolved against [getApplicationDocumentsDirectory]. Absolute paths + /// are used as-is. /// /// Also registers the flutter type adapters /// - [colorAdapterTypeId] - The type id for the color adapter (default: 200) /// - [timeOfDayAdapterTypeId] - The type id for the time of day adapter (default: 201) Future initFlutter([ - String? subDir, + String? dir, HiveStorageBackendPreference backendPreference = HiveStorageBackendPreference.native, int? colorAdapterTypeId, @@ -24,8 +26,15 @@ extension HiveX on HiveInterface { String? path; if (!kIsWeb) { - final appDir = await getApplicationDocumentsDirectory(); - path = path_helper.join(appDir.path, subDir); + // Root-relative Windows paths still take the drive from the app directory + if (dir != null && + path_helper.isAbsolute(dir) && + !path_helper.isRootRelative(dir)) { + path = dir; + } else { + final appDir = await getApplicationDocumentsDirectory(); + path = path_helper.join(appDir.path, dir); + } } init( diff --git a/hive_flutter/lib/src/wrapper/stub/path.dart b/hive_flutter/lib/src/wrapper/stub/path.dart index fab62db4..945fcf29 100644 --- a/hive_flutter/lib/src/wrapper/stub/path.dart +++ b/hive_flutter/lib/src/wrapper/stub/path.dart @@ -13,3 +13,17 @@ String join( '[Hive Error] Tried to use the `path` package from Flutter Web.', ); } + +/// Stub `path.isAbsolute` method +bool isAbsolute(String path) { + throw UnimplementedError( + '[Hive Error] Tried to use the `path` package from Flutter Web.', + ); +} + +/// Stub `path.isRootRelative` method +bool isRootRelative(String path) { + throw UnimplementedError( + '[Hive Error] Tried to use the `path` package from Flutter Web.', + ); +} diff --git a/hive_flutter/pubspec.yaml b/hive_flutter/pubspec.yaml index 6ac20570..7208d060 100644 --- a/hive_flutter/pubspec.yaml +++ b/hive_flutter/pubspec.yaml @@ -1,6 +1,6 @@ name: hive_ce_flutter description: Extension for Hive. Makes it easier to use Hive in Flutter apps. -version: 2.3.4 +version: 2.4.0 homepage: https://github.com/IO-Design-Team/hive_ce/tree/main/hive_flutter environment: