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.
- In our
JobListingController
controller class,lib\job_listing.dart
, we define a new variable of the typeHttp: final Http _http;
. The constructor now becomes the following:JobListingController(this._http) { _loadData(); }
The bulk of the change takes place in...