fetchGoogleSuggestions method
- String language = "",
Fetches Google suggestions based on the given language. Returns a list of suggestions as strings. If the language is not provided, it defaults to an empty string.
Implementation
Future<List<String>> fetchGoogleSuggestions({String language = ""}) async {
if (isNotBlank) {
final url = Uri.https('suggestqueries.google.com', '/complete/search', {
'output': 'toolbar',
if (language.isNotBlank) 'hl': language,
'q': this,
'gl': 'in',
});
try {
final response = await http.get(url);
if (response.statusCode == 200) {
// Parse the XML response
final xmlData = response.body;
// Extract suggestions from the XML (you can use an XML parsing library)
// For simplicity, let's assume the suggestions are separated by '<suggestion data="..."/>'
final suggestionRegex = RegExp(r'<suggestion data="([^"]+)"');
final matches = suggestionRegex.allMatches(xmlData);
final suggestions = matches.map((match) => match.group(1)?.urlDecode.trimAll).distinct().toList();
return suggestions.nonNulls.toList();
}
} catch (e) {
consoleLog('Error fetching suggestions: $e');
}
}
return [];
}