flutter_google_places not showing autocomplete search results

7,190

Solution 1

I figured it out. The _search() function has a radius radius: center == null ? null : 10000); that it uses to calculate areas around you within the radius you give it. My emulator has my location as being in the middle of the Atlantic Ocean (separate issue, but very minor). Since there were no places that start with 'grand' near my location in the middle of the ocean, nothing was coming up. I increased the radius from 10000 to 10000000 and it started to find locations.

Solution 2

Look in the console for errors. In my case, my billing was linked and relevent APIs was enabled, still it never show predictions. Then I added the following strictBounds & types as non-null values.

Prediction p = await PlacesAutocomplete.show(
                  context: context,
                  apiKey: Local.googleMapsAPIKey,
                  radius: 10000000,
                  types: [],
                  strictbounds: false,
                  mode: Mode.overlay,
                  language: "fr",
                  decoration: InputDecoration(
                    hintText: 'Search',
                    focusedBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(20),
                      borderSide: BorderSide(
                        color: Colors.white,
                      ),
                    ),
                  ),
                  components: [Component(Component.country, "fr")],
                );

Solution 3

Check if your billing its activate on Google APIS

Share:
7,190
evanhaus
Author by

evanhaus

Updated on December 11, 2022

Comments

  • evanhaus
    evanhaus over 1 year

    I am following along with the tutorial from the following link on using Google Maps API to create a search function to look for places on a map:

    Medium.com flutter tutorial

    Below is the code that I believe constitutes the core of the issue:

     /**
    * Retrieves the user's location
    * NOTE: This pretty much does the same thing as _animateToUser,
    * but I separated the two
    */
    Future<LatLng> getUserLocation() async {
     var currentLocation = <String, double>{};
     final uLocation = LocationManager.Location();
    try {
      currentLocation = await uLocation.getLocation();
      final lat = currentLocation["latitude"];
      final lng = currentLocation["longitude"];
      final center = LatLng(lat, lng);
      return center;
    } on Exception {
      currentLocation = null;
      return null;
    }
    }
    
    /**
     * Searches for locations using Google Maps API
    */
    Future<void> _search() async {
    try {
      final center = await getUserLocation();
      Prediction p = await PlacesAutocomplete.show(
          context: context,
          strictbounds: center == null ? false : true,
          apiKey: kGoogleApiKey,
          onError: onError,
          mode: Mode.overlay,
          language: "en",
          location: center == null
              ? null
              : Location(center.latitude, center.longitude),
          radius: center == null ? null : 10000);
    
      showDetailPlace(p.placeId);
    } catch (e) {
      return;
    }
    }
    
    /**
    * Shows details of a place
    */
    Future<Null> showDetailPlace(String placeId) async {
    if (placeId != null) {
      Navigator.push(
        context,
        MaterialPageRoute(builder: (context) => 
        PlaceDetailWidget(placeId)),
      );
     }
     } /**
     * Retrieves the user's location
     * NOTE: This pretty much does the same thing as _animateToUser,
     * but I separated the two
     */
    Future<LatLng> getUserLocation() async {
    var currentLocation = <String, double>{};
    final uLocation = LocationManager.Location();
    try {
      currentLocation = await uLocation.getLocation();
      final lat = currentLocation["latitude"];
      final lng = currentLocation["longitude"];
      final center = LatLng(lat, lng);
      return center;
    } on Exception {
      currentLocation = null;
      return null;
    }
    }`
    

    The problem is that, no matter what I type into the search bar, it will not autocomplete/suggest anything.

    Example: enter image description here

    In this example I want to bring up a list of places that have the word grand in them like 'Grand Central Station', for instance.