From f80a0fbd0203b453d50e432215e548d89de15b08 Mon Sep 17 00:00:00 2001 From: Istvan Soos Date: Thu, 16 Jan 2025 16:08:40 +0100 Subject: [PATCH] use nulls and different break logic --- app/lib/service/youtube/backend.dart | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/app/lib/service/youtube/backend.dart b/app/lib/service/youtube/backend.dart index 48f62601e..829cf3925 100644 --- a/app/lib/service/youtube/backend.dart +++ b/app/lib/service/youtube/backend.dart @@ -122,23 +122,17 @@ class _PkgOfWeekVideoFetcher { try { final pageTokensVisited = {}; - String nextPageToken = ''; + String? nextPageToken; final videos = []; final videoIds = {}; while (videos.length < 50) { - // check visited status - if (pageTokensVisited.contains(nextPageToken)) { - break; - } - pageTokensVisited.add(nextPageToken); - // get page from cache or from Youtube API - final rs = await cache.youtubePlaylistItems(nextPageToken).get( + final rs = await cache.youtubePlaylistItems(nextPageToken ?? '').get( () async => await youtube.playlistItems.list( ['snippet', 'contentDetails'], playlistId: powPlaylistId, - pageToken: nextPageToken.isEmpty ? null : nextPageToken, + pageToken: nextPageToken, ), ); @@ -177,8 +171,13 @@ class _PkgOfWeekVideoFetcher { } } + pageTokensVisited.add(nextPageToken ?? ''); // advance to next page token - nextPageToken = rs.nextPageToken ?? ''; + nextPageToken = rs.nextPageToken; + if (nextPageToken == null || + pageTokensVisited.contains(nextPageToken)) { + break; + } } return videos; } finally {