onStart method

  1. @override
void onStart()
override

Callback from the app when this task is to be started.

Implementation

@override
void onStart() {
  // first initialize the background task executor
  super.onStart();

  // then check for permission to access health data
  try {
    var healthProbe = backgroundTaskExecutor.probes
        .firstWhere((probe) => probe is HealthProbe) as HealthProbe;

    healthProbe.hasPermissions().then((granted) {
      if (granted) {
        debug(
            '$runtimeType - Already has permissions to access health data.');
        _startProbeAndStopAgain();
      } else {
        healthProbe.requestPermissions().then((granted) {
          if (granted) {
            debug('$runtimeType - Got permissions to access health data.');
            _startProbeAndStopAgain();
          } else {
            warning(
                '$runtimeType - Could not get permissions to access health data.');
            return;
          }
        });
      }
    });
  } catch (error) {
    // if the health probe is not found in the list of probes, we cannot
    // access health data.
    warning(
        '$runtimeType - No health probe found in list of probes. Does the '
        'study protocol include any health data types?');
  }
}