validateAddress function

Future<String> validateAddress(
  1. String rawAddress,
  2. ToolContext? toolContext, {
  3. GoogleMapsConfig? config,
})

Validates an address and saves the classification to the ADK session.

Implementation

Future<String> validateAddress(
  String rawAddress,
  ToolContext? toolContext, {
  GoogleMapsConfig? config,
}) async {
  if (rawAddress.trim().isEmpty) {
    return 'Cannot validate an empty address.';
  }

  final PropertyClassification classification;
  try {
    classification = await classifyPropertyAddressAsync(
      rawAddress,
      config: config,
    );
  } on MapsApiError catch (exc) {
    return 'Address validation failed: $exc.';
  }

  if (toolContext != null) {
    toolContext.state['address_classification'] = classification.toJson();
    toolContext.state['is_multi_unit'] =
        classification.addressType == AddressType.multiUnit;
    toolContext.state['is_missing_subunit'] = classification.isMissingSubunit;
    toolContext.state['standardized_address'] =
        classification.standardizedAddress;
  }

  final List<String> report = <String>[
    '### Address Validation',
    '- **Standardized Address**: ${classification.standardizedAddress}',
    '- **Base Address**: ${classification.baseAddress}',
    '- **Property Type**: ${classification.addressType.value}',
    '- **Location Type**: ${classification.locationType}',
  ];
  if (classification.subunit != null && classification.subunit!.isNotEmpty) {
    report.add('- **Sub-unit**: ${classification.subunit}');
  }
  if (classification.isMissingSubunit) {
    report.add(
      '- **Warning**: Address appears to be a multi-unit property '
      'with a missing unit/apartment number.',
    );
  }

  return report.join('\n');
}