Book Image

DART Cookbook

By : Ivo Balbaert
Book Image

DART Cookbook

By: Ivo Balbaert

Overview of this book

Table of Contents (18 chapters)
Dart Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using a service


The following step is to read the data from a JSON file, which we will use in this recipe. Angular has a built-in core functionality called the HTTP Service to make HTTP requests to a server. In our example, the job data has been serialized to the JSON format in the file jobs.json, and we will make an HTTP request to the web server to get this data. You can follow along with the code in the project angular_service.

How to do it...

The change we make in this recipe is nearly transparent to the user; the web page stays the same, but because making an HTTP request is asynchronous, we will work with a Future and must provide a message, such as "Loading data…", as long as the request is being executed.

  1. In our JobListingController controller class, lib\job_listing.dart, we define a new variable of the type Http: final Http _http; . The constructor now becomes the following:

    JobListingController(this._http) {
        _loadData();
    }

    The bulk of the change takes place in its _loadData()...