I’m trying to make a simple Flutter application which has a user sign in to Spotify and aggregate listening activity, show stats, etc.
I’m having some trouble with the Spotify API. Every time I try launch the Spotify authorization URL I’m getting the following error message from Flutter.
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(Error, Error while launching https://accounts.spotify.com/authorize/?response_type=code&client_id=6b12ed8ef79648e88e6646d7f16f5a50&scope=user-read-private&redirect_uri=http://localhost:8888//callback, null, null)
Here’s what my code for getting the URL looks like.
Future<void> login(BuildContext context) async {
await dotenv.load();
final clientId = dotenv.env['SPOTIFY_CLIENT_ID'];
final scopes = dotenv.env['SPOTIFY_SCOPES'];
final redirectUri = dotenv.env['SPOTIFY_REDIRECT_URI'];
final url = Uri.parse('https://accounts.spotify.com/authorize/'
'?response_type=code'
'&client_id=$clientId'
'&scope=$scopes'
'&redirect_uri=$redirectUri');
// Launch the constructed URL using url_launcher
await launchUrl(url);
}
where my environment variables are
SPOTIFY_CLIENT_ID=6b12ed8ef79648e88e6646d7f16f5a50
SPOTIFY_SECRET_ID=...
SPOTIFY_REDIRECT_URI=http://localhost:8888//callback
SPOTIFY_SCOPES=user-read-private
(side note: is it fine to share my client ID? I’ve removed my secret ID)
I’ve messed around with the redirect URI to no avail. And I have double-checked that my redirect URI is the same as my Spotify developer platform.
The eventual goal is to get a successful login, get the user access token, and then navigate out of the web authorization page.
Thanks in advance for the help.




