求助:flutter youtube_explode_dart包文档存在错误,使用时遇两处核心报错
Hey there, I’ve worked with this package before, so let’s break down your two errors and fix them step by step:
1. Error accessing video.title
The core issue here is that yt.videos.get() returns a Future<Video>, not a direct Video object. You can’t access properties like title immediately without resolving the future first.
Corrected Code:
// Make sure this code lives inside an async function Future<void> fetchVideoDetails() async { var yt = YoutubeExplode(); var video = await yt.videos.get('https://youtube.com/watch?v=Dpp1sIL1m5Q'); var title = video.title; // This will work now! print(title); // Don't forget to clean up resources when done yt.close(); }
Almost all core methods in youtube_explode_dart rely on network calls and return futures, so you’ll always need await to get the actual data.
2. Error: streamManifest is undefined
You’re trying to use streamManifest without first fetching it from the YouTube API. You need to explicitly retrieve the stream manifest using the streamsClient.getManifest() method (which is also asynchronous). Also, there’s a small typo in your code: withHigestVideoQuality() should be withHighestVideoQuality() (note the extra 'h').
Corrected Code:
Future<void> fetchStreamInfo() async { var yt = YoutubeExplode(); // First, fetch the stream manifest for the video var streamManifest = await yt.videos.streamsClient.getManifest('https://youtube.com/watch?v=Dpp1sIL1m5Q'); // Then grab the highest quality muxed stream var streamInfo = streamManifest.muxed.withHighestVideoQuality(); print(streamInfo.url); yt.close(); }
Quick Pro Tip
Wrap your YoutubeExplode usage in a try-finally block if you’re handling errors, to ensure the client is always closed even if something goes wrong:
Future<void> safeFetch() async { var yt = YoutubeExplode(); try { // Your code here } catch (e) { print('Error: $e'); } finally { yt.close(); } }
内容的提问来源于stack exchange,提问作者Programmerz

