How to pass authorization token from storage to header in flutter

Viewed 19

Hey guys please i have this issue of passing token fetched from storage in flutter using flutter_secure_storage to header but it doesn't work, if i assign the token to a different key in the Map it works but when assign to Authorization it throws an ERROR 'Null' is not a subtype of type 'Map<dynamic, dynamic>'

my code for storing and retrieving token

 Future setToken(String token)async{
   await _storage.write(key: 'Bearer', value: token);
}
  Future<String?>get_bearer_key() async{
     String? token = await _storage.read(key: 'Bearer');
     return token;
  }

my code to pass token to header

 String? key = await get_bearer_key();

      try{

        Map<String,String> requestHeader = new Map();
        requestHeader['Content-type'] = 'application/json';
        requestHeader['Accept'] = 'application/json';
        requestHeader['Authorization'] = '$key';
        requestHeader['Request-Channel'] = 'mobile';

      if(activation_code == null){
        return setState(() {
          error = "missing credentials";
        });
      }

      var url = Uri.parse('$baseUrl/users/activate-account');
      final body = jsonEncode({
        "activation_code": activation_code,
      });

     Response reponse = await put(url,body:body,
          headers: requestHeader
      );

      Map data = jsonDecode(reponse.body);
      print(data);
0 Answers
Related