So basically I have a Future which return the jwt I got from secure storage, and I want to assign the return to a variable let's say the variable is token. It goes kind of something like this.
Future<String> getJwt() async {
final secureStorage = SecureStorage();
var jwt = await secureStorage.readSecureData('jwt');
return jwt;
}
and I want to assign to variable, like this.
static String token = getJwt();
the code is something like this.
String? _token;
Future<String> getJwt() async {
final secureStorage = SecureStorage();
var jwt = await secureStorage.readSecureData('jwt');
return jwt;
}
void getJWT() async {
String token = await getJwt();
}
class API {
final token = getJWT();
static Future<String> getData(String url) async {
try {
http.Response response = await http.get(Uri.parse(baseURL + url),
headers: {
"Content-type": "application/json",
"Authorization": 'Bearer ' + token
});
return response.body;
} catch (_) {
return 'error';
}
}
static Future<String> postData(String url, String json) async {
try {
http.Response response = await http.post(Uri.parse(baseURL + url),
headers: {
"Content-type": "application/json",
'Authorization': 'Bearer ' + token
},
body: json);
return response.body;
} catch (_) {
return 'error';
}
}
}
I already try changing the String to Future but It doesn't work, how to solve this problem? thanks in advance.