Skip to content

Instantly share code, notes, and snippets.

@AyeshaIftikhar
Last active June 25, 2021 11:56
Show Gist options
  • Save AyeshaIftikhar/38eef88ba753b216aaa2bd7722904ffe to your computer and use it in GitHub Desktop.
Save AyeshaIftikhar/38eef88ba753b216aaa2bd7722904ffe to your computer and use it in GitHub Desktop.
Implementing REST API's in Flutter

Accessing/Implementing REST API's

Fetching data from the internet is necessary for most apps. Luckily, Dart and Flutter provide tools, such as the http package, for this type of work. http is a Future-based library and uses await and async features. It provides many high level methods and simplifies the development of REST based mobile applications.

Basic Concepts

https provides a high level class and http to do web requests.

  • http class provides functionality to perform all types of HTTP requests.
  • http methods accept a url, and additional information through Dart Map (post data, additional headers, etc.,). It requests the server and collects the response back in async/await pattern. For example, the below code reads the data from the specified url and print it in the console.

Some core methods are:

  • read: Request the specified url through GET method and return back the response as Future
  • get: Request the specified url through GET method and return back the response as Future. Response is a class holding the response information.
  • post: Request the specified url through POST method by posting the supplied data and return back the response as Future
  • put: Request the specified url through PUT method and return back the response as Future
  • head: Request the specified url through HEAD method and return back the response as Future
  • delete: Request the specified url through DELETE method and return back the response as Future

http also provides a more standard HTTP client class, client. Client class: client supports persistent connection. It will be useful when a lot of request to be made to a particular server. It needs to be closed properly using close method.

Methods Details

http.get()

The http.get() method returns a Future that contains a Response.

  • Future is a core Dart class for working with async 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.

_ Although it’s convenient, it’s not recommended to put an API call in a build() method._

Parse JSON in the Background

By default, Dart apps do all of their work on a single thread. In many cases, this model simplifies coding and is fast enough that it does not result in poor app performance or stuttering animations, often called “jank.”

However, you might need to perform an expensive computation, such as parsing a very large JSON document. If this work takes more than 16 milliseconds, your users experience jank.

To avoid jank, you need to perform expensive computations like this in the background. On Android, this means scheduling work on a different thread. In Flutter, you can use a separate Isolate.

Working with isolates

Isolates communicate by passing messages back and forth. These messages can be primitive values, such as null, num, bool, double, or String, or simple objects such as the List in this example.

You might experience errors if you try to pass more complex objects, such as a Future or http.Response between isolates.

Credits: Flutter.dev

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment