getForecast static method

ProgressListener getForecast({
  1. required List<WeatherDurationCoordinates> coords,
  2. void onComplete(
    1. GemError,
    2. List<LocationForecast> locationForecasts
    )?,
})

Async gets forecast weather for a list of coordinates and durations.

Parameters

  • IN coords The coordinates & timestamps list for which the forecast is requested.
  • IN onComplete The callback which will be called when the operation completes.
    • Will be called with GemError.success error and non-empty locationForecasts upon success.
    • Will be called with GemError.invalidInput error and empty locationForecasts if the coordinates list is empty.
    • Will be called with GemError.resourceMissing error and empty locationForecasts if the internal engine resource is missing
    • Will be called with GemError.outOfRange error and empty locationForecasts if number of coordinates is greater than the maximum allowed.
    • Will be called with other GemError values and empty locationForecasts on other errors

Returns

Implementation

static ProgressListener getForecast({
  required final List<WeatherDurationCoordinates> coords,
  void Function(
    GemError,
    List<LocationForecast> locationForecasts,
  )? onComplete,
}) {
  for (final WeatherDurationCoordinates coord in coords) {
    if (coord.coordinates.latitude.abs() > 90 ||
        coord.coordinates.longitude.abs() > 180) {
      onComplete?.call(GemError.invalidInput, <LocationForecast>[]);

      return EventDrivenProgressListener();
    }
  }

  final LocationForecastList result = LocationForecastList.create();
  final EventDrivenProgressListener listener = EventDrivenProgressListener();
  GemKitPlatform.instance.registerEventHandler(listener.id, listener);

  listener.registerOnCompleteWithData((
    final int err,
    final String hint,
    final Map<dynamic, dynamic> json,
  ) {
    GemKitPlatform.instance.unregisterEventHandler(listener.id);
    if (err == 0) {
      onComplete?.call(GemErrorExtension.fromCode(err), result.getJson());
      result.dispose();
    } else {
      onComplete?.call(
        GemErrorExtension.fromCode(err),
        <LocationForecast>[],
      );
    }
  });
  final OperationResult resultString = staticMethod(
    'Weather',
    'getForecast',
    args: <String, dynamic>{
      'coords': coords,
      'result': result.id,
      'listener': listener.id,
    },
  );
  final GemError errCode = GemErrorExtension.fromCode(resultString['result']);
  if (errCode != GemError.success) {
    onComplete?.call(errCode, <LocationForecast>[]);
  }
  return listener;
}