I don't know of the title is completely appropriate, but here's what I actually want to do. I have urls which point to mp3 files, now some are valid and and some are not, as of now, so before I render the controls for playback of those audio files in my UI, I want to check whether the URL will give me the mp3 or result in a 404. How can I do that?
Make an HTTP request and check the response code
final response =
await http.get(Uri.parse('https://jsonplaceholder.typicode.com/posts/1'));
if (response.statusCode == 200) {
See https://flutter.io/docs/cookbook/networking/fetch-data for more details.
A HEAD
request will be more efficient though when you only want to check if the URL is valid because it doesn't actually download content.
final response =
await http.head(Uri.parse('https://jsonplaceholder.typicode.com/posts/1'));
if (response.statusCode == 200) {
See also https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With