Using the HTTP service to retrieve books

Our app will need to connect to the service through HTTP, so the first step we need to perform is adding the latest version of the http package in the pubspec.yaml file. As we are editing this file, let's also add support for shared_preferences, which we'll use later in the chapter. As usual, please make sure you check the latest version on the https://pub.dev website.

In the dependencies node of pubspec.yaml, add the support for HTTP and shared_preferences, as follows:

http: ^0.12.0+4 
shared_preferences: ^0.5.6+1

In the lib/data folder of our app, let's create a new file called books_helper.dart that will contain the class that builds the queries to the Google Books API.

For a refresher on how to connect to a web service and work with JSON, have a look at Chapter 5, Let's Go to the Movies - Getting Data from the Web in the Connecting to a web service and retrieving data with HTTP section.

In the file, we'll need the http package for the connection, the dart:convert package, to decode the JSON data, the dart:async library to use asynchronous methods, material.dart for the navigation, shared_preferences to save data locally (more on that later), and, of course, the Book class.

Let's start building the books_helper.dart file, as follows:

  1. Add the following code to add the required imports:
import 'package:http/http.dart' as http; 
import 'package:flutter/material.dart';
import 'dart:convert';
import 'dart:async';
import 'package:http/http.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'book.dart';
  1. Still in the bookshelper.dart file, create a class called BooksHelper, like this:
class BooksHelper { } 
  1. Inside the BooksHelper class, create a few constants to build the URL of the query. urlKey will contain your Google Books API key, urlQuery contains the user query, and urlBase is the fixed part to retrieve information from the web service, as shown in the following code snippet:
final String urlKey = '&key=[ADD YOUR KEY HERE]'; 
final String urlQuery = 'volumes?q=';
final String urlBase = 'https://www.googleapis.com/books/v1/';
  1. Now, create a new method called getBooks(). This will take a String containing the books the user is looking for and will return a Future for a List of dynamic items, as follows:
Future<List<dynamic>> getBooks(String query) async {} 
  1. In the getBooks() method, create the full url containing the query and the key, like this:
final String url = urlBase + urlQuery + query + urlKey; 
  1. Once the url String is ready, we can leverage the http library to call the get() method that will retrieve the books data. This is asynchronous and returns a Response object, so here, we'll use the await statement to get the result, like this:
Response result = await http.get(url);
  1. If the status of the response is successful (statusCode 200), we'll decode the body of the result into a variable called jsonResponse. In particular, we need a node called items from the body, which contains the volume's information.

Once the items node is retrieved, just call the map() method over it, and for each volume in the items node, create a Book from the json object and then return a List of the books that were created.

  1. If the status of the Response is not successful, then this method returns null. Add the following code in the getBooks() method to retrieve the data from the Google Books API:
    if (result.statusCode == 200) { 
      final jsonResponse = json.decode(result.body);
      final booksMap = jsonResponse['items'];
      List<dynamic> books = booksMap.map((i) => Book.fromJson(i)).toList();
      return books;
    }
    else {
      return null;
    }

Now that we have written the method to search books from the Google Books API, let's add a user interface (UI) so that we can show some results to our user!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.189.251