Skip to content

Commit 93ccc13

Browse files
authored
Merge pull request #219 from dart-lang/mit-mit-example
Add a small example
2 parents ffe786a + 7aef285 commit 93ccc13

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

example/main.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import 'dart:convert' as convert;
2+
import 'package:http/http.dart' as http;
3+
4+
main(List<String> arguments) async {
5+
// This example uses the Google Books API to search for books about http.
6+
// https://developers.google.com/books/docs/overview
7+
var url = "https://www.googleapis.com/books/v1/volumes?q={http}";
8+
9+
// Await the http get response, then decode the json-formatted responce.
10+
var response = await http.get(url);
11+
if (response.statusCode == 200) {
12+
var jsonResponse = convert.jsonDecode(response.body);
13+
var itemCount = jsonResponse['totalItems'];
14+
print("Number of books about http: $itemCount.");
15+
} else {
16+
print("Request failed with status: ${response.statusCode}.");
17+
}
18+
}

0 commit comments

Comments
 (0)