Reputation: 11676
I'm developing a flutter app and trying to use flutter_secure_storage with hydrated_bloc. To summarise the packages, flutter_secure_storage
is an abstraction to encrypt data on the device, and hydrated_bloc
is used with flutter_bloc to persist bloc/cubits (from a runtime store). You can customise the read/write interface of hydrated_bloc
, so I figured that I could use them together.
The example on the hydared_bloc page works as expected. Adjusting the counter up and down, then exiting the app and coming back, shows the counter is being read back on startup and is clearly persisting.
But if I add a "Custom Hydrated Storage" with read/write capabilities from flutter_secure_storage
, all data is lost on restarting of the app. Here is my "Custom Hydrated Storage":
import 'dart:convert';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:hydrated_bloc/hydrated_bloc.dart';
class CustomHydratedStorage implements Storage {
final _storage = const FlutterSecureStorage();
@override
dynamic read(String key) async {
final value = await _storage.read(
key: key,
iOptions: const IOSOptions(accessibility: KeychainAccessibility.first_unlock),
aOptions: const AndroidOptions(
encryptedSharedPreferences: true,
),
);
print('Reading $key and got $value');
return value;
}
@override
Future<void> write(String key, dynamic value) async {
print('Writing $key = $value');
await _storage.write(key: key, value: value);
}
@override
Future<void> delete(String key) async {
// TODO: implement delete
print('HydratedStorage: delete ($key)');
}
@override
Future<void> clear() async {
// TODO: implement clear
print('HydratedStorage: clear');
}
@override
Future<void> close() async {
// TODO: implement close
print('HydratedStorage: close');
}
}
Am I missing something here? Is flutter_secure_storage
too secure, i.e more configuration is needed in order to use it?
Upvotes: 0
Views: 981
Reputation: 1234
you could use built-in encryption in HydratedBloc :
const password = 'hydration';
final byteskey = sha256.convert(utf8.encode(password)).bytes;
HydratedBloc.storage = await HydratedStorage.build(
storageDirectory: await getApplicationDocumentsDirectory(),
encryptionCipher: HydratedAesCipher(byteskey));
Upvotes: 0
Reputation: 3
I ran into the same problem, after some investigation in the source code of hydration bloc I think I found what is the problem. Flutter secure storage reads the values async but Hive (the default storage used in the hydration bloc) reads values sync. So when the state is being read the result is not being awaited and is always null
Upvotes: 0