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