From 8aa1052bd70376485ef8821f2ef3122eaf0fa1ed Mon Sep 17 00:00:00 2001 From: hana8-8 Date: Sat, 2 May 2026 18:53:13 +0300 Subject: [PATCH 1/2] pagination probelm fixed --- .../playlist_remote_datasource.dart | 56 ++++++++++++++----- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/lib/features/playlist/data/datasources/playlist_remote_datasource.dart b/lib/features/playlist/data/datasources/playlist_remote_datasource.dart index 99fdb03d..7752a8fd 100644 --- a/lib/features/playlist/data/datasources/playlist_remote_datasource.dart +++ b/lib/features/playlist/data/datasources/playlist_remote_datasource.dart @@ -282,26 +282,52 @@ class PlaylistRemoteDatasource { // ============================================================ // ── FETCH: Tracks Inside a Playlist // ============================================================ - Future> fetchPlaylistTracks( + Future> fetchPlaylistTracks( String playlistId, { - int page = 1, int limit = 20, }) async { - _log('→ GET /playlists/$playlistId/tracks page=$page limit=$limit'); - try { - final response = await _dio.get>( - '/playlists/$playlistId/tracks', - queryParameters: {'page': page, 'limit': limit}, + final allTracks = []; + int page = 1; + bool hasNext = true; + const int maxPages = 20; // safety cap — 20 pages × 20 = 400 tracks max + + while (hasNext && page <= maxPages) { + _log( + '→ GET /playlists/$playlistId/tracks page=$page limit=$limit', ); - _log('← ${response.statusCode}'); - final data = response.data!['data'] as Map; - final trackList = data['tracks'] as List; - _log('← Got ${trackList.length} tracks for playlist $playlistId'); - return PlaylistTrackModel.fromJsonList(trackList); - } on DioException catch (e) { - _logError('fetchPlaylistTracks($playlistId) failed', e); - rethrow; + try { + final response = await _dio.get>( + '/playlists/$playlistId/tracks', + queryParameters: {'page': page, 'limit': limit}, + ); + _log('← ${response.statusCode}'); + + final data = response.data!['data'] as Map; + final trackList = data['tracks'] as List; + final pagination = data['pagination'] as Map?; + + _log( + '← Page $page: ${trackList.length} tracks' + ' has_next=${pagination?['has_next']}', + ); + + final parsed = PlaylistTrackModel.fromJsonList(trackList); + allTracks.addAll(parsed); + + // Check if there are more pages + hasNext = pagination?['has_next'] as bool? ?? false; + page++; + } on DioException catch (e) { + _logError('fetchPlaylistTracks($playlistId) page=$page failed', e); + rethrow; + } } + + _log( + '← fetchPlaylistTracks done: ${allTracks.length} total tracks ' + 'across ${page - 1} page(s)', + ); + return allTracks; } // ============================================================ From 4b3637c9fee649a50fcbefa153ec7b9d1e82d483 Mon Sep 17 00:00:00 2001 From: hana8-8 Date: Sat, 2 May 2026 19:20:08 +0300 Subject: [PATCH 2/2] format line --- .../playlist_remote_datasource.dart | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/lib/features/playlist/data/datasources/playlist_remote_datasource.dart b/lib/features/playlist/data/datasources/playlist_remote_datasource.dart index 7752a8fd..9d745fd2 100644 --- a/lib/features/playlist/data/datasources/playlist_remote_datasource.dart +++ b/lib/features/playlist/data/datasources/playlist_remote_datasource.dart @@ -282,7 +282,7 @@ class PlaylistRemoteDatasource { // ============================================================ // ── FETCH: Tracks Inside a Playlist // ============================================================ - Future> fetchPlaylistTracks( + Future> fetchPlaylistTracks( String playlistId, { int limit = 20, }) async { @@ -290,30 +290,28 @@ class PlaylistRemoteDatasource { int page = 1; bool hasNext = true; const int maxPages = 20; // safety cap — 20 pages × 20 = 400 tracks max - + while (hasNext && page <= maxPages) { - _log( - '→ GET /playlists/$playlistId/tracks page=$page limit=$limit', - ); + _log('→ GET /playlists/$playlistId/tracks page=$page limit=$limit'); try { final response = await _dio.get>( '/playlists/$playlistId/tracks', queryParameters: {'page': page, 'limit': limit}, ); _log('← ${response.statusCode}'); - + final data = response.data!['data'] as Map; final trackList = data['tracks'] as List; final pagination = data['pagination'] as Map?; - + _log( '← Page $page: ${trackList.length} tracks' ' has_next=${pagination?['has_next']}', ); - + final parsed = PlaylistTrackModel.fromJsonList(trackList); allTracks.addAll(parsed); - + // Check if there are more pages hasNext = pagination?['has_next'] as bool? ?? false; page++; @@ -322,7 +320,7 @@ class PlaylistRemoteDatasource { rethrow; } } - + _log( '← fetchPlaylistTracks done: ${allTracks.length} total tracks ' 'across ${page - 1} page(s)',