1

[ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: RangeError (index): Invalid value: Valid value range is empty: 0

**E/flutter (11420): #0 List.[] (dart:core-patch/growable_array.dart:264:36)

E/flutter (11420): #1 LocationService.getPlaceId (package:ridesharingv1/models/location_service.dart:15:37)

E/flutter (11420): **

    import 'dart:convert' as convert;

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

class LocationService {
  // LocationService();
  final String _apiKey = 'AIza...';

  Future<String> getPlaceId(String input) async {
    final String url =
        'https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=$input&inputted=textquery&fields=place_id&key=$_apiKey';
    var response = await http.get(Uri.parse(url));
    // if (response.statusCode == 200) {
    var json = convert.jsonDecode(response.body);
    var placeId = json['candidates'][0]['place_id'] as String;
    print(placeId);
    return placeId;
    // } else {
    //   throw Exception('Failed to load place id');
    // }
  }

  // Future<Map<String, dynamic>> getPlace(String input) async {
  //   final placeId = await getPlace(input);
  //   final String url =
  //       'https://maps.googleapis.com/maps/api/place/details/json?place_id=$placeId&fields=name,formatted_address,geometry&key=$_apiKey';
  //   var response = await http.get(Uri.parse(url));
  //
  //   // if (response.statusCode == 200) {
  //   final json = convert.jsonDecode(response.body);
  //
  //   var result = json['result'] as Map<String, dynamic>;
  //   print(result);
  //   return result;
  //   // } else {
  //   //   throw Exception('Failed to load place');
  //   // }
  // }
}

1 Answer 1

1

,You just have no json['candidates'][0]. Try to print json and look at it's structure:

    var json = convert.jsonDecode(response.body);
    //var placeId = json['candidates'][0]['place_id'] as String;
    print(json);
    return "";

Also look if You enable Billing on the Google Cloud Project. If no it will return empty structure. And be careful, don't publish Your API key:

    final String _apiKey = 'AIz......';

Generate the new one, so nobody can use it!

Have a nice time!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.