From fafa9a21a455811b2e824cca7f078d36f0eb4be3 Mon Sep 17 00:00:00 2001 From: Jason Held Date: Fri, 6 Mar 2026 08:58:10 -0500 Subject: [PATCH 1/3] Rename subDir to dir in initFlutter, treat as relative or absolute --- hive_flutter/lib/src/hive_extensions.dart | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/hive_flutter/lib/src/hive_extensions.dart b/hive_flutter/lib/src/hive_extensions.dart index d11776db..4fc6a618 100644 --- a/hive_flutter/lib/src/hive_extensions.dart +++ b/hive_flutter/lib/src/hive_extensions.dart @@ -6,15 +6,15 @@ import 'package:hive_ce_flutter/src/wrapper/path.dart' as path_helper; /// Flutter extensions for Hive. extension HiveX on HiveInterface { - /// Initializes Hive with the path from [getApplicationDocumentsDirectory]. + /// Initializes Hive with the path from [getApplicationDocumentsDirectory], or as an absolute path. /// - /// You can provide a [subDir] where the boxes should be stored. + /// You can provide a [dir] where the boxes should be stored. /// /// 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 +24,18 @@ extension HiveX on HiveInterface { String? path; if (!kIsWeb) { - final appDir = await getApplicationDocumentsDirectory(); - path = path_helper.join(appDir.path, subDir); + // join accepts the latter arguments as nullable so it is safe + // to consider it "relative" even in that case. If the user wants an + // absolute path pointing to the base of a well-defined + // path (including one with built-in dart support), + // different from getApplicationDocumentsDirectory, + // then they should pass that directory in. + if (dir == null || path_helper.isRelative(dir)) { + final appDir = await getApplicationDocumentsDirectory(); + path = path_helper.join(appDir.path, dir); + } else { + path = dir; + } } init( From de9e778ff46452913517b9afb9a7015bb5866298 Mon Sep 17 00:00:00 2001 From: Rexios Date: Sun, 27 Sep 2026 13:41:24 -0400 Subject: [PATCH 2/3] Keep drive for root-relative paths and stub new path functions on web --- hive_flutter/CHANGELOG.md | 4 ++++ hive_flutter/lib/src/hive_extensions.dart | 21 ++++++++++----------- hive_flutter/lib/src/wrapper/stub/path.dart | 14 ++++++++++++++ hive_flutter/pubspec.yaml | 2 +- 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/hive_flutter/CHANGELOG.md b/hive_flutter/CHANGELOG.md index 520f6dbe..0e00b12a 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` (thanks [@jheld](https://github.com/jheld)) + ## 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 4fc6a618..e6e353ca 100644 --- a/hive_flutter/lib/src/hive_extensions.dart +++ b/hive_flutter/lib/src/hive_extensions.dart @@ -6,9 +6,11 @@ import 'package:hive_ce_flutter/src/wrapper/path.dart' as path_helper; /// Flutter extensions for Hive. extension HiveX on HiveInterface { - /// Initializes Hive with the path from [getApplicationDocumentsDirectory], or as an absolute path. + /// Initializes Hive with the path from [getApplicationDocumentsDirectory]. /// - /// You can provide a [dir] 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) @@ -24,17 +26,14 @@ extension HiveX on HiveInterface { String? path; if (!kIsWeb) { - // join accepts the latter arguments as nullable so it is safe - // to consider it "relative" even in that case. If the user wants an - // absolute path pointing to the base of a well-defined - // path (including one with built-in dart support), - // different from getApplicationDocumentsDirectory, - // then they should pass that directory in. - if (dir == null || path_helper.isRelative(dir)) { + // 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); - } else { - path = dir; } } 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: From e9598c024e1294bf7de0300eaf6e0db4114ac622 Mon Sep 17 00:00:00 2001 From: Rexios Date: Sun, 27 Sep 2026 13:46:39 -0400 Subject: [PATCH 3/3] Match changelog credit style --- hive_flutter/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hive_flutter/CHANGELOG.md b/hive_flutter/CHANGELOG.md index 0e00b12a..91129170 100644 --- a/hive_flutter/CHANGELOG.md +++ b/hive_flutter/CHANGELOG.md @@ -1,6 +1,6 @@ ## 2.4.0 -- `Hive.initFlutter` uses absolute paths as-is without calling `getApplicationDocumentsDirectory` (thanks [@jheld](https://github.com/jheld)) +- `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