简体   繁体   中英

Flutter Secure Storage

How to get data from secure storage in any other function not in FutureBuilder.

    Future<void> login() async {
      print('LOGGGGINNNNNNNNNNNN');
      print(read('TOKEN'));
    }

    Future read(String storageName) async {
      return await _storage.read(key: storageName);
    }

Print result: Instance of 'Future'

You should do this instead

    Future<void> login() async {
      print('LOGGGGINNNNNNNNNNNN');
      print(await read('TOKEN'));
    }

    Future read(String storageName) async {
      return _storage.read(key: storageName);
    }

Since read returns a Future it will print what you currently see, so you need to wait its completion.

Solution

Future readStorage(String storageName) async {
  return await _storage.read(key: storageName);
}

 Future getToken() async {
  final _storage = new FlutterSecureStorage();
  await Future.sync(()  async =>  _token = await _storage.read(key: 'TOKEN'));
 }

Future<void> login() async {
  getToken().then((value){
  print(_token);
  if(_token == 'Not Authenticated' || _token == null)
    {
      setState(() {
        _isAuthenticated = false;
      });
    }
  else {
    setState(() {
      _isAuthenticated = true;
    });
  }
});
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM