api method
Gets an instance of the api.
If the credentials have expired, a new set are seemlessly requested via the refresh token.
Implementation
Future<SpotifyApi?> api() async {
final expiration = _prefs.getInt(_SPOTIFY_EXPIRATION_KEY) ?? 0;
final now = DateTime.now().millisecondsSinceEpoch;
if (this._spotifyApi == null || expiration < now) {
final String? accessToken = _prefs.getString(_SPOTIFY_ACCESS_KEY);
final String? refreshToken = _prefs.getString(_SPOTIFY_REFRESH_KEY);
if (accessToken is String && refreshToken is String) {
final creds = SpotifyApiCredentials(
this._clientId,
this._clientSecret,
accessToken: accessToken,
refreshToken: refreshToken,
scopes: _scopes,
expiration: DateTime.fromMillisecondsSinceEpoch(expiration),
);
try {
this._spotifyApi = SpotifyApi(creds);
} on AuthorizationException catch (e) {
print('ERROR: $e');
this._spotifyApi = null;
}
_saveCredentials();
}
}
return this._spotifyApi;
}