onStart method

  1. @override
Future<bool> onStart()
inherited

Callback when this executor is started. Returns true if successfully started, false otherwise.

Implementation

@override
Future<bool> onStart() async {
  if (await requestPermissions()) {
    Duration? interval = samplingConfiguration?.interval;
    Duration? duration = samplingConfiguration?.duration;
    if (interval != null && duration != null) {
      _timer = Timer.periodic(interval, (Timer t) {
        onSamplingStart();
        _bufferingStreamSubscription = bufferingStream.listen(onSamplingData,
            onError: _onError, onDone: _onDone);
        _durationTimer = Timer(duration, () async {
          await _bufferingStreamSubscription?.cancel();
          onSamplingEnd();
          try {
            Measurement? measurement = await getMeasurement();
            if (measurement != null) addMeasurement(measurement);
          } catch (error) {
            addError(error);
          }
        });
      });
    } else {
      warning(
          '$runtimeType - no valid interval and duration found in sampling configuration: $samplingConfiguration. '
          'Is a valid PeriodicSamplingConfiguration provided?');
      return false;
    }
    return true;
  } else {
    return false;
  }
}