I have this construction in my homepage widget which is suppose to get the saved token. Now this returns a Future. So How can I read the token string? I tried storedToken.toString() but it returns Future<String?> not what I expected. I am expecting a String value like "fhsdjkfhs.djkfhsdjkfhsdjk"
void initState(){
const _storageToken = FlutterSecureStorage();
storedToken = _storageToken.read(key: 'jwt');
print(storedToken);
}
CodePudding user response:
_storageToken.read() returns a Future, so you have to await it in order to get the value.
The problem is, you can't perform async/await action directly on the initState. If you still want to call that _storageToken.read() on the initState, you can use this workaround.
void initState(){
/// Initialize Flutter Secure Storage
const _storageToken = FlutterSecureStorage();
/// Await your Future here (This function only called once after the layout is Complete)
WidgetsBinding.instance?.addPostFrameCallback((timeStamp) async {
final storedToken = await _storageToken.read(key: 'jwt');
print(storedToken);
});
}