getPlaces method

Future<ApiResponse<List<MapBoxPlace>>> getPlaces(
  1. String queryText, {
  2. Proximity proximity = const NoProximity(),
  3. CancelToken? cancelToken,
})

Get the places for the given query text

Implementation

Future<ApiResponse<List<MapBoxPlace>>> getPlaces(
  String queryText, {
  Proximity proximity = const NoProximity(),
  CancelToken? cancelToken,
}) async {
  final path = _basePath + Uri.encodeFull(queryText) + '.json';

  final Map<String, dynamic> queryParameters = {
    ...switch (proximity) {
      (LocationProximity l) => {"proximity": l.asString},
      (IpProximity ip) => {"proximity": ip.asString},
      (NoProximity _) => {},
    },
  };
  try {
    final response = await _dio.get(
      path,
      queryParameters: queryParameters,
      cancelToken: cancelToken,
    );

    if (response.statusCode != 200) {
      return (
        success: null,
        failure: FailureResponse.fromJson(response.data)
      );
    } else {
      return (
        success: Predictions.fromJson(response.data).features,
        failure: null
      );
    }
  } on DioException catch (e) {
    return (
      success: null,
      failure: FailureResponse(
        message: e.message ?? 'Unknown error',
        error: e.error.toString(),
        code: e.response?.statusCode.toString(),
      ),
    );
  } catch (e) {
    return (
      success: null,
      failure: FailureResponse(
        message: e.toString(),
        error: e.toString(),
        code: '500',
      ),
    );
  }
}