The APIService class: requesting an access token


Important note about Android Release apk builds.

Some students have reported that running requests with the APIService implemented in this video leads to SocketException errors in release mode.

Here's a relevant StackOverflow thread:

Flutter Http error SocketException: OS Error: Connection refused

To fix this, follow these two steps:

  • Ensure that the AndroidManifest.xml file contains the Internet permission: uses-permission android:name="android.permission.INTERNET"
  • Add an 'Accept': 'application/json' header when making the request:

final response = await api.tokenUri().toString(), headers: { 'Accept': 'application/json', 'Authorization': 'Basic ${api.apiKey}' }, );