getRequest method

void getRequest(
  1. String searchText,
  2. BuildContext buildContext
)

Implementation

void getRequest(String searchText,BuildContext buildContext) async {

  if (widget.googleKey.isEmpty) {
    return;
  }

  searchText = searchText.replaceAll(' ', '%20');
  String url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=$searchText"
      "&location=19.265138,73.072425"
      "&radius=10000"
      "&key=${widget.googleKey}";
  var response = await Locations().getSearchAddress(url,context);

  try {
    Map obj = json.decode(response);

    if (locationList != null && locationList!.isNotEmpty) {
      locationList!.clear();
    }

    //pick the top records only
    int len = obj['results'].length;
    if (len > 5) {
      len = 5;
    }
    for (int i = 0; i < len; i++) {
      OrderAddress orderAddress = OrderAddress();

      final tagName = obj['results'][i]['formatted_address'];
      final split = tagName!.split(',');
      final Map<int, String> values = {for (int i = 0; i < split.length; i++) i: split[i]};

      if (values[0] == obj['results'][i]['name']) {
        orderAddress.loc = obj['results'][i]['formatted_address'];
      } else {
        orderAddress.loc = obj['results'][i]['name'] + '-' + obj['results'][i]['formatted_address'];
      }

      orderAddress.lat = '${obj['results'][i]['geometry']['location']['lat']}';
      orderAddress.lng = '${obj['results'][i]['geometry']['location']['lng']}';

      locationList!.add(orderAddress); //save the search data in list to select searched address from list
    }
    searchAddressProgressFlag = false;
    setState(() {});
  } catch (exc) {
    debugPrint(exc.toString());
  }
}