· 5 years ago · Aug 22, 2020, 07:48 PM
1import 'dart:convert';
2import 'package:hospital_app/models/hospitalModel.dart';
3import 'package:http/http.dart';
4
5class HttpService {
6 final String postsURL =
7 "https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJKSp50NI0K4gR6C6L3yYqpYY&fields=name&key=AIzaSyBkq8usKgRT2zGqvmqsEFNoWoS9elHTLhM";
8
9//I'm using same url from example with my API key. Also enabled Maps JavaScript API & Places API & Geocoding API & Geolocation API for my API key. Google maps page was worked with this API key.
10
11 Future<List<Map<String, dynamic>>> getPosts() async {
12 Response res = await get(postsURL);
13
14 if (res.statusCode == 200) {
15 print('RESBODY: ${res.body}');
16 final parsed = jsonDecode(res.body);
17
18 Future<List<Map<String, dynamic>>> posts = parsed
19 .map(
20 (dynamic item) => Hospital.fromJson(item),
21 )
22 .toList();
23
24 return posts;
25 } else {
26 throw "Can't get posts.";
27 }
28 }
29}
30
31
32class Hospital {
33 final String address;
34 final double longitude;
35 final double latitude;
36
37 Hospital({
38 @required this.address,
39 @required this.longitude,
40 @required this.latitude,
41 });
42
43 factory Hospital.fromJson(Map<String, dynamic> json) {
44 return Hospital(
45 address: json['address'] as String,
46 longitude: json['longitude'] as double,
47 latitude: json['latitude'] as double,
48 );
49 }
50}
51
52
53
54ERROR MESSAGE:
55
56I/flutter ( 9963): RESBODY: {
57I/flutter ( 9963): "error_message" : "This API project is not authorized to use this API.",
58I/flutter ( 9963): "html_attributions" : [],
59I/flutter ( 9963): "status" : "REQUEST_DENIED"
60I/flutter ( 9963): }
61