I work on Music App with Flutter. I want to get music data from YouTube with Youtube API but I just want to list the music. How can I do this on flutter? Music CategoryId = 10 but how can I make a query with this? (Note: I get the Youtube API.)
The code below is an API service code that I have coded:
import 'dart:convert';import 'dart:io';import 'package:http/http.dart' as http;import 'package:beatifun/models/channe_model.dart';import 'package:beatifun/models/video_model.dart';import 'package:beatifun/utilities/keys.dart';class APIService { APIService._instantiate(); static final APIService instance = APIService._instantiate(); final String _baseUrl = 'www.googleapis.com'; String _nextPageToken = ''; Future<Channel> fetchChannel({String channelId}) async { Map<String, String> parameters = {'part': 'snippet, contentDetails, statistics','id': channelId,'key': API_KEY, }; Uri uri = Uri.https( _baseUrl,'/youtube/v3/channels', parameters, ); Map<String, String> headers = { HttpHeaders.contentTypeHeader: 'application/json', }; // Get Channel var response = await http.get(uri, headers: headers); if (response.statusCode == 200) { Map<String, dynamic> data = json.decode(response.body)['items'][0]; Channel channel = Channel.fromMap(data); // Fetch first batch of videos from uploads playlist channel.videos = await fetchVideosFromPlaylist( playlistId: channel.uploadPlaylistId, ); return channel; } else { throw json.decode(response.body)['error']['message']; } } Future<List<Video>> fetchVideosFromPlaylist({String playlistId}) async { Map<String, String> parameters = {'part': 'snippet','playlistId': playlistId,'maxResults': '8','pageToken': _nextPageToken,'key': API_KEY, }; Uri uri = Uri.https( _baseUrl,'/youtube/v3/playlistItems', parameters, ); Map<String, String> headers = { HttpHeaders.contentTypeHeader: 'application/json', }; // Get Playlist Videos var response = await http.get(uri, headers: headers); if (response.statusCode == 200) { var data = json.decode(response.body); _nextPageToken = data['nextPageToken'] ?? ''; List<dynamic> videosJson = data['items']; // Fetch first eight videos from uploads playlist List<Video> videos = []; videosJson.forEach( (json) => videos.add( Video.fromMap(json['snippet']), ), ); return videos; } else { throw json.decode(response.body)['error']['message']; } } Future<List<Video>> fetchVideosFromCategory({int categoryId}) async { Map<String, dynamic> parameters = {'part': 'snippet','categoryId': categoryId,'maxResults': '80','pageToken': _nextPageToken,'key': API_KEY, }; Uri uri = Uri.https( _baseUrl,'/youtube/v3/videos', parameters, ); Map<String, String> headers = { HttpHeaders.contentTypeHeader: 'application/json', }; // Get Playlist Videos var response = await http.get(uri, headers: headers); if (response.statusCode == 200) { var data = json.decode(response.body); _nextPageToken = data['nextPageToken'] ?? ''; List<dynamic> videosJson = data['items']; // Fetch first eight videos from uploads playlist List<Video> videos = []; videosJson.forEach( (json) => videos.add( Video.fromMap(json['snippet']), ), ); return videos; } else { throw json.decode(response.body)['error']['message']; } }}