1

I am getting the error E/Volley (21144): [88] cx.a: Unexpected response code 400 for https://clients.google.com/glm/map/api after I started using Google Maps in my Flutter app. Does anyone know why I am getting this error. I copies the url from a youtuber so I don't know if the url should be unique or if it is universal...The code:

import 'package:http/http.dart' as http;
import 'dart:convert' as convert;

class LocationService {
  final String key = 'KEY_HERE';

  Future<String> getPlaceId(String input) async {
    final String url =
        'https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=$input&inputtype=textquery&key=$key';
    var response = await http.get(Uri.parse(url));
    var json = convert.jsonDecode(response.body);
    // if (json['candidates'].isEmpty) {
    //   // handle error when no place found
    //   throw Exception('No place found for the input: $input');
    // }
    //when i run this if statement it throws the Exception
    var placeId = json['candidates'][0]['place_id'] as String;
    return placeId;
  }

  Future<Map<String, dynamic>> getPlace(String input) async {
    final placeId = await getPlaceId(input);

    final String url =
        'https://maps.googleapis.com/maps/api/place/details/json?place_id=$placeId&key=$key';

    var response = await http.get(Uri.parse(url));
    var json = convert.jsonDecode(response.body);
    var results = json['result'] as Map<String, dynamic>;

    return results;
  }
}

In my Maps.dart file:

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(
        title: Text('Googel Maps'),
      ),
      body: Column(children: [
        Row(children: [
          Expanded(
              child: TextFormField(
            controller: _searchController,
            textCapitalization: TextCapitalization.words,
            decoration: InputDecoration(hintText: 'Search for Place'),
            onChanged: (value) {},
          )),
          IconButton(
              onPressed: () async {
                var place =
                    await LocationService().getPlace(_searchController.text);
                _goToPlace(place);
              //This is the place where i am typing and it should get the location
              },
              icon: Icon(Icons.search)),
        ]),
        Expanded(
          child: GoogleMap(
            mapType: MapType.normal,
            markers: {
              _kGooglePlexMarker,
              // _kLakeMarker
            },
            initialCameraPosition: _kGooglePlex,
            onMapCreated: (GoogleMapController controller) {
              _controller.complete(controller);
            },
          ),
        ),
      ]),
    );
  }

  Future<void> _goToPlace(Map<String, dynamic> place) async {
    final double lat = place['geometry']['location']['lat'];
    final double lng = place['geometry']['location']['lng'];

    final GoogleMapController controller = await _controller.future;
    controller.animateCamera(
      CameraUpdate.newCameraPosition(
        CameraPosition(target: LatLng(lat, lng), zoom: 12),
      ),
    );
  }

It keeps returning the error: E/Volley (21144): [88] cx.a: Unexpected response code 400 for https://clients.google.com/glm/map/api

What I have tried: I have tried added the SHA-1 and Flutter Project Name to the Google Maps Project. I am coping this code from this Youtube video (seeing as it is the first time I am using this API) = https://www.youtube.com/watch?v=tfFByL7F-00 But the video could be more detailed and is very rushed in my opinion.

What I think the problem is: The url is returning a Bad report 400 I do think that the url is incorrect but I do not know where to get the correct url.

Tyrone
  • 17
  • 7
  • 1
    Please never share your own API keys or others' API keys again in public forums :). I edited the question, hopefully it'll get approved soon! – bqubique Mar 26 '23 at 22:13
  • 1
    I completely forgot to remove it thx so much! – Tyrone Mar 26 '23 at 22:14
  • Did you have a look at https://stackoverflow.com/questions/61331519/unexpected-response-code-400-from-google-maps-in-flutter? – Almis Mar 27 '23 at 00:00
  • @Almis Yes that was the first thing I did, I added the SHA-1 and name of the flutter project. – Tyrone Mar 27 '23 at 10:15

0 Answers0