Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions hive_flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
17 changes: 13 additions & 4 deletions hive_flutter/lib/src/hive_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> initFlutter([
String? subDir,
String? dir,
HiveStorageBackendPreference backendPreference =
HiveStorageBackendPreference.native,
int? colorAdapterTypeId,
Expand All @@ -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(
Expand Down
14 changes: 14 additions & 0 deletions hive_flutter/lib/src/wrapper/stub/path.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
);
}
2 changes: 1 addition & 1 deletion hive_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down