textSearch method
Text search for places
Implementation
Future<List<LocationResult>> textSearch({
required String query,
String? region, // ccTLD two-character value
}) async {
if (!isInitialized) {
throw Exception('Google Places API key not initialized. Call initialize() first.');
}
try {
final uri = Uri.parse('$_baseUrl/textsearch/json').replace(queryParameters: {
'query': query,
'key': _apiKey!,
if (region != null) 'region': region,
});
final response = await http.get(uri);
if (response.statusCode == 200) {
final data = json.decode(response.body);
if (data['status'] == 'OK') {
final results = data['results'] as List<dynamic>;
return results.map((result) {
final geometry = result['geometry'];
final location = geometry['location'];
return LocationResult.fromGooglePlace(
result,
location['lat'].toDouble(),
location['lng'].toDouble(),
);
}).toList();
} else {
throw Exception('Google Places API error: ${data['status']} - ${data['error_message'] ?? 'Unknown error'}');
}
} else {
throw Exception('HTTP error: ${response.statusCode}');
}
} catch (e) {
throw Exception('Failed to search places: $e');
}
}