how-to-store-different-types-of-data-in-flutter-secure-storage
Problem
I am new to flutter and I am using flutter secure storage to store user credentials. so I searched on the internet that how can I store different types of data into flutter secure storage as it is allowing me to store only string types of data. I want to store boolean values like login into it, but I did not find any proper answer. Please, someone, confirm me, can I store or not different types of data also into it, instead of String data.
Solution
Assuming you're talking about the flutter_secure_storage package: it supports strings only.
If you want to store other types of data, you can encode them as a string. The most convenient way to do that is probably JSON. You can use jsonEncode and jsonDecode from the dart:convert library
Yo can use some sort of type parsing. For bool:
To write to Secure Storage:
await _secureStorage.write(key: _key, value: boolValue.toString());
To read from Secure Storage:
var value = await _secureStorage.red(key: _key);
Value will be of type String, you can then "cast" it to bool:
bool boolValue = value.toLowerCase() == 'true';
.toLowerCase()
just in case, somehow in Secure Storage will be True.
For int you can do:
int i = int.parse('42');
Top comments (0)