searchForSentences method
- {required BuildContext context,
- required AppModel appModel,
- required String searchTerm}
Search Tatoeba for example sentences and return a list of results.
Implementation
Future<List<String>> searchForSentences({
required BuildContext context,
required AppModel appModel,
required String searchTerm,
}) async {
if (searchTerm.trim().isEmpty) {
return [];
}
Language language = appModel.targetLanguage;
String langCode = language.threeLetterCode;
if (_tatoebaCache[langCode] != null) {
return _tatoebaCache[langCode]!;
}
List<String> sentences = [];
http.Response response = await _client.get(Uri.parse(
'https://tatoeba.org/en/api_v0/search?from=$langCode&has_audio=&native=&orphans=no&query=${Uri.encodeComponent(searchTerm)}&sort=relevance&sort_reverse=&tags=&to=none&trans_filter=limit&trans_has_audio=&trans_link=&trans_orphan=&trans_to=&trans_unapproved=&trans_user=&unapproved=no&user='));
Map<String, dynamic> json = jsonDecode(response.body);
List<Map<String, dynamic>> results =
List<Map<String, dynamic>>.from(json['results']);
sentences = results.map((result) => result['text'].toString()).toList();
_tatoebaCache[langCode] = sentences;
return sentences;
}