Skip to content

Commit 1e5a1e2

Browse files
committed
[hive_flutter] Use conditional imports of io dependencies
Improves pub score by conditionally importing path and path_provider
1 parent 867f31a commit 1e5a1e2

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

hive_flutter/lib/hive_flutter.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import 'dart:async';
55
import 'package:flutter/foundation.dart';
66
import 'package:flutter/widgets.dart';
77
import 'package:hive/hive.dart';
8-
import 'package:path_provider/path_provider.dart';
9-
import 'package:path/path.dart' as path_helper;
8+
import 'package:path_provider/path_provider.dart'
9+
if (dart.library.html) 'src/stub/path_provider.dart';
10+
import 'package:path/path.dart' if (dart.library.html) 'src/stub/path.dart'
11+
as path_helper;
1012

1113
export 'package:hive/hive.dart';
1214

hive_flutter/lib/src/hive_extensions.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ extension HiveX on HiveInterface {
55
/// Initializes Hive with the path from [getApplicationDocumentsDirectory].
66
///
77
/// You can provide a [subDir] where the boxes should be stored.
8-
Future initFlutter([String? subDir]) async {
8+
Future<void> initFlutter([String? subDir]) async {
99
WidgetsFlutterBinding.ensureInitialized();
10-
if (!kIsWeb) {
11-
var appDir = await getApplicationDocumentsDirectory();
12-
init(path_helper.join(appDir.path, subDir));
13-
}
10+
if (kIsWeb) return;
11+
12+
var appDir = await getApplicationDocumentsDirectory();
13+
init(path_helper.join(appDir.path, subDir));
1414
}
1515
}

hive_flutter/lib/src/stub/path.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
String join(
2+
String part1, [
3+
String? part2,
4+
String? part3,
5+
String? part4,
6+
String? part5,
7+
String? part6,
8+
String? part7,
9+
String? part8,
10+
]) {
11+
throw UnimplementedError(
12+
'[Hive Error] Tried to use the `path` package from Flutter Web.',
13+
);
14+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
abstract class Directory {
2+
String get path;
3+
}
4+
5+
Future<Directory> getApplicationDocumentsDirectory() {
6+
throw UnimplementedError(
7+
'[Hive Error] Tried to use the `path_provider` package from Flutter Web.',
8+
);
9+
}

0 commit comments

Comments
 (0)