Zum Inhalt

Fetch Data from the Internet

Key Points

  • Fetching data from the internet is usually executed asynchronously using specific libraries the allow for making HTTP requests.
  • Data fetched from APIs are usually encoded in the JSON format and need to mapped internally to a custom data structure. This process is called deserialization.
  • In order to make HTTP requests set the corresponding permissions (Android) and/or entitlements (macOS).
  • Use the dart:html package to make HTTP requests.
  • The response is usually stored in a Future.
  • A Future is a core Dart class for working with asynchronous operations. A Future object represents a potential value or error that will be available at some time in the future.
  • The http.Response class contains the data received from a successful http call.
  • To display the data on screen, use the FutureBuilder widget. The FutureBuilder widget comes with Flutter and makes it easy to work with asynchronous data sources.
  • Bad practice: Do not place or execute API calls in build() methods.
  • Before you request data from an API, test it using common API/endpoint test tools or IDE extensions.

Recommended Learning Resources