Skip to content

Commit

Permalink
Merge pull request #222 from bvasya/fix-null-audio-feature
Browse files Browse the repository at this point in the history
add null check before fromJson to endpoint_base.dart
  • Loading branch information
rinukkusu authored Oct 29, 2024
2 parents 3022a09 + ed5be99 commit a609807
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/src/endpoints/endpoint_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ abstract class EndpointBase {
required T Function(Map<String, dynamic>) fromJson}) async {
var jsonString = await _api._get(path);
final tJson = jsonDecode(jsonString)[jsonKey] as Iterable<dynamic>;
return tJson.map((json) => fromJson(json));
return tJson.where((e) => e != null).map((json) => fromJson(json));
}
}
25 changes: 25 additions & 0 deletions test/data/v1/audio-features.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"audio_features": [
{
"acousticness": 0.011,
"analysis_url": "https://api.spotify.com/v1/audio-analysis/11dFghVXANMlKmJXsNCbNl",
"danceability": 0.696,
"duration_ms": 207960,
"energy": 0.905,
"id": "11dFghVXANMlKmJXsNCbNl",
"instrumentalness": 0.000905,
"key": 2,
"liveness": 0.302,
"loudness": -2.743,
"mode": 1,
"speechiness": 0.103,
"tempo": 114.944,
"time_signature": 4,
"track_href": "https://api.spotify.com/v1/tracks/11dFghVXANMlKmJXsNCbNl",
"type": "audio_features",
"uri": "spotify:track:11dFghVXANMlKmJXsNCbNl",
"valence": 0.625
},
null
]
}
20 changes: 20 additions & 0 deletions test/data/v1/audio-features/11dFghVXANMlKmJXsNCbNl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"acousticness": 0.011,
"analysis_url": "https://api.spotify.com/v1/audio-analysis/11dFghVXANMlKmJXsNCbNl",
"danceability": 0.696,
"duration_ms": 207960,
"energy": 0.905,
"id": "11dFghVXANMlKmJXsNCbNl",
"instrumentalness": 0.000905,
"key": 2,
"liveness": 0.302,
"loudness": -2.743,
"mode": 1,
"speechiness": 0.103,
"tempo": 114.944,
"time_signature": 4,
"track_href": "https://api.spotify.com/v1/tracks/11dFghVXANMlKmJXsNCbNl",
"type": "audio_features",
"uri": "spotify:track:11dFghVXANMlKmJXsNCbNl",
"valence": 0.625
}
6 changes: 6 additions & 0 deletions test/data/v1/audio-features/2cs7JxrZ9DxvsfoVI07ayX.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"error": {
"status": 404,
"message": "analysis not found"
}
}
16 changes: 16 additions & 0 deletions test/spotify_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ Future main() async {
});
});

group('Audio Features', () {
test('get', () async {
var audioFeatures =
await spotify.audioFeatures.get('11dFghVXANMlKmJXsNCbNl');

expect(audioFeatures.id, '11dFghVXANMlKmJXsNCbNl');
});

test('list', () async {
var audioFeatures = await spotify.audioFeatures
.list(['11dFghVXANMlKmJXsNCbNl', '2cs7JxrZ9DxvsfoVI07ayX']);

expect(audioFeatures.length, 1);
});
});

group('Audio Analysis', () {
test('get', () async {
var result = await spotify.audioAnalysis.get('xyz123');
Expand Down

0 comments on commit a609807

Please sign in to comment.