search method
Future<List<FMData> >
search({
- FMSearchOptions? options,
- required String searchText,
- dynamic onError()?,
Search addresses using query
Implementation
Future<List<FMData>> search({
FMSearchOptions? options,
required String searchText,
Function(Object, StackTrace)? onError,
}) async {
if (searchText.trim().isEmpty) return [];
options ??= FMSearchOptions.initial();
try {
final box = await _calculateBoundingBox(options.radius);
final query = {
'format': 'jsonv2',
'polygon_geojson': 1,
'q': searchText.trim(),
'limit': options.limit,
'accept-language': options.langs.map((e) => e.trim()).join(','),
if (options.countries != null)
'countrycodes': options.countries?.map((e) => e.trim()).join(','),
if (box != null) 'bounded': 1,
if (box != null) 'viewbox': '${box[0]},${box[1]},${box[2]},${box[3]}',
};
String url = _baseURL;
url += _searchEndpoint;
url += '?${query.keys.map((k) => '$k=${query[k]}').join('&')}';
final res = await http.get(Uri.parse(url));
final data = jsonDecode(res.body) as List;
if (data.isEmpty && box == null) throw Exception('No data');
return data.map((e) => FMRawData.fromJSON(e).data).toList();
} catch (e, st) {
if (options.maxRetries > 0) {
return search(
searchText: searchText,
options: FMSearchOptions.reducedRetry(options),
);
}
if (onError != null) onError(e, st);
return [];
}
}