onStart method

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

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) {
      // create a recurrent timer that every [interval] starts the buffering
      timer = Timer.periodic(interval, (Timer t) {
        onSamplingStart();
        // create a timer that stops the buffering after the specified [duration].
        Timer(duration, () async {
          onSamplingEnd();
          // collect the measurement
          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;
  }
}