You need to use async/await with your methods to get the values.
you can do the following
Future<void> refreshToken() async {
final http.Response _response = await _httpClient.callApi(
endPoint: refreshTokenEndpoint,
variables: refreshTokenVariables,
);
print(_response.body);
}
The reason that yours will not work is because you are not awaiting the call. You have the right idea, you just need to specify in your code that _httpClient.callApi
is going to take some time to return a value. When you do that, your code will run asynchronously rather than synchronously. You can read more here.
When you run your code this is what is happening
void main() async {
var str = 'this is going to run first';
// executes immediately
print(str);
// executes immediately
// this will waits one second before printing
Future.delayed(Duration(seconds: 1)).then((_) {
str = 'this will print third after 1 second of waiting';
print(str);
});
// executes immediately
// this print statement will print second because [Future] has not finished
// completed the await of the 1 sec duration
str = 'this is going to print second';
print(str);
}
Everything is executing immediately but because we have a second delay, its function doesn't get executed after the future has awaited the 1 sec duration.
Therefore, we can see that you are assigning _tokenResponse
with the response from your request, but because you are not awaiting the result, your code moves on and your value gets left behind.