getLocationResults method

Future<List<Address>> getLocationResults(
  1. String inputText
)

Implementation

Future<List<Address>> getLocationResults(String inputText) async {
  if (_sessionToken == null && widget.sessionToken == true) {
    setState(() {
      _sessionToken = uuid.v4();
    });
  }

  if (inputText.isEmpty) {
    setState(() {
      FlutterGooglePlacesWeb.showResults = false;
    });
  } else {
    setState(() {
      FlutterGooglePlacesWeb.showResults = true;
    });
  }

  String baseURL = 'https://maps.googleapis.com/maps/api/place/autocomplete/json';
  String type = 'address';
  String input = Uri.encodeComponent(inputText);
  if (widget.proxyURL == null) {
    proxiedURL = '$baseURL?input=$input&key=${widget.apiKey}&type=$type&sessiontoken=$_sessionToken';
  } else {
    proxiedURL = '${widget.proxyURL}$baseURL?input=$input&key=${widget.apiKey}&type=$type&sessiontoken=$_sessionToken';
  }
  if (widget.offset == null) {
    offsetURL = proxiedURL;
  } else {
    offsetURL = proxiedURL + '&offset=${widget.offset}';
  }
  if (widget.components == null) {
    componentsURL = offsetURL;
  } else {
    componentsURL = offsetURL + '&components=${widget.components}';
  }
  print(componentsURL);
  final Map<String, dynamic> response = await http.get(Uri.parse('$componentsURL'), headers: {'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': 'true', 'Access-Control-Allow-Methods': 'GET,POST,HEAD'}).then((value) => jsonDecode(value.body));
  // print(response);
  var predictions = response['predictions'];
  if (predictions != []) {
    displayedResults.clear();
  }

  for (var i = 0; i < predictions.length; i++) {
    String name = predictions[i]['description'];
    String streetAddress = predictions[i]['structured_formatting']['main_text'];
    List<dynamic> terms = predictions[i]['terms'];
    String city = terms[terms.length - 2]['value'];
    String country = terms[terms.length - 1]['value'];
    displayedResults.add(Address(
      name: name,
      streetAddress: streetAddress,
      city: city,
      country: country,
    ));
  }

  return displayedResults;
}