Line data Source code
1 : import 'dart:async'; 2 : import 'dart:io'; 3 : 4 : import 'package:hive/hive.dart'; 5 : import 'package:path/path.dart' as path_helper; 6 : 7 : import 'hive_local_storage.dart'; 8 : 9 : class IoHiveLocalStorage implements HiveLocalStorage { 10 1 : IoHiveLocalStorage({this.baseDirFn, List<int> encryptionKey, this.clear}) 11 : : encryptionCipher = 12 1 : encryptionKey != null ? HiveAesCipher(encryptionKey) : null; 13 : 14 1 : @override 15 1 : HiveInterface get hive => Hive; 16 : @override 17 : final HiveAesCipher encryptionCipher; 18 : final FutureOr<String> Function() baseDirFn; 19 : final bool clear; 20 : 21 : bool _isInitialized = false; 22 : 23 : @override 24 1 : Future<void> initialize() async { 25 1 : if (_isInitialized) return this; 26 : 27 1 : if (baseDirFn == null) { 28 1 : throw UnsupportedError(''' 29 : A base directory path MUST be supplied to 30 : the hiveLocalStorageProvider via the `baseDirFn` 31 : callback. 32 : 33 : In Flutter, `baseDirFn` will be supplied automatically if 34 : the `path_provider` package is in `pubspec.yaml` AND 35 : Flutter Data is properly configured: 36 : 37 : If using Riverpod, did you supply the override? 38 : 39 : Widget build(context) { 40 : return ProviderScope( 41 : overrides: [ 42 : configureRepositoryLocalStorage() 43 : ], 44 : child: MaterialApp( 45 : 46 : If using Provider, did you include the providers? 47 : 48 : Widget build(context) { 49 : return MultiProvider( 50 : providers: [ 51 : ...repositoryProviders(), 52 : ], 53 : child: MaterialApp( 54 : '''); 55 : } 56 : 57 4 : final dir = Directory(await baseDirFn()); 58 2 : final exists = await dir.exists(); 59 1 : if ((clear ?? true) && exists) { 60 2 : await dir.delete(recursive: true); 61 : } 62 : 63 2 : final path = path_helper.join(dir.path, 'flutter_data'); 64 2 : hive..init(path); 65 : 66 1 : _isInitialized = true; 67 : return this; 68 : } 69 : } 70 : 71 1 : HiveLocalStorage getHiveLocalStorage( 72 : {FutureOr<String> Function() baseDirFn, 73 : List<int> encryptionKey, 74 : bool clear}) { 75 1 : return IoHiveLocalStorage( 76 : baseDirFn: baseDirFn, encryptionKey: encryptionKey, clear: clear); 77 : }