From d9057dae574ee5bc2d41baf0c43cb171cb741da9 Mon Sep 17 00:00:00 2001 From: Kingkor Roy Tirtho Date: Sun, 16 Mar 2025 10:32:41 +0600 Subject: [PATCH] fix: invalid access token exception #2525 --- lib/provider/spotify/playlist/featured.dart | 29 +++++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/lib/provider/spotify/playlist/featured.dart b/lib/provider/spotify/playlist/featured.dart index 69057e5d..9df8a5d1 100644 --- a/lib/provider/spotify/playlist/featured.dart +++ b/lib/provider/spotify/playlist/featured.dart @@ -30,12 +30,31 @@ class FeaturedPlaylistsNotifier @override fetch(int offset, int limit) async { - final playlists = await spotify.playlists.featured.getPage( - limit, - offset, - ); + try { + final playlists = await spotify.playlists.featured.getPage( + limit, + offset, + ); - return playlists.items?.toList() ?? []; + return playlists.items?.toList() ?? []; + } catch (e) { + /// This check only needs to be done once. Since this is one of the very first + /// request + /// + /// If the token is invalid, we refresh it and retry the request. + /// Same goes for expired tokens + if ((e is AuthorizationException && e.error == 'invalid_token') || + e is ExpirationException) { + await ref.read(authenticationProvider.notifier).refreshCredentials(); + + final playlists = await spotify.playlists.featured.getPage( + limit, + offset, + ); + return playlists.items?.toList() ?? []; + } + rethrow; + } } @override