build static method
Future<HydratedStorage>
build({
- required Directory storageDirectory,
- HydratedCipher? encryptionCipher,
Returns an instance of HydratedStorage.
storageDirectory
is required.
For web, use webStorageDirectory as the storageDirectory
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:hydrated_bloc_integration/hydrated_bloc_integration.dart';
import 'package:path_provider/path_provider.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
final storage = await HydratedStorage.build(
storageDirectory: kIsWeb
? HydratedStorage.webStorageDirectory
: await getTemporaryDirectory(),
);
HydratedBlocOverrides.runZoned(
() => runApp(App()),
storage: storage,
);
}
With encryptionCipher
you can provide custom encryption.
Following snippet shows how to make default one:
import 'package:crypto/crypto.dart';
import 'package:hydrated_bloc_integration/hydrated_bloc_integration.dart';
const password = 'hydration';
final byteskey = sha256.convert(utf8.encode(password)).bytes;
return HydratedAesCipher(byteskey);
Implementation
static Future<HydratedStorage> build({
required Directory storageDirectory,
HydratedCipher? encryptionCipher,
}) {
return _lock.synchronized(() async {
if (_instance != null) return _instance!;
// Use HiveImpl directly to avoid conflicts with existing Hive.init
// https://github.com/hivedb/hive/issues/336
hive = HiveImpl();
Box<dynamic> box;
if (storageDirectory == webStorageDirectory) {
box = await hive.openBox<dynamic>(
'hydrated_box',
encryptionCipher: encryptionCipher,
);
} else {
hive.init(storageDirectory.path);
box = await hive.openBox<dynamic>(
'hydrated_box',
encryptionCipher: encryptionCipher,
);
await _migrate(storageDirectory, box);
}
return _instance = HydratedStorage(box);
});
}