fetch method

Future<List<NexusSurvey>> fetch()

Fetch eligible surveys from the server (targeting/sampling/capping applied).

Implementation

Future<List<NexusSurvey>> fetch() async {
  try {
    final props = <String, Object?>{..._cfg.defaultProperties, ..._id.traits};
    final res = await _http.post('/partner/surveys/active', {
      if (_id.distinctId != null) 'distinctId': _id.distinctId,
      'deviceKey': _id.deviceKey,
      if (props.isNotEmpty) 'properties': props,
      if (_id.deviceContext['osType'] != null)
        'osType': _id.deviceContext['osType'],
    });
    final list = (res?['surveys'] as List?) ?? const [];
    _active = list.map(NexusSurvey.fromJson).toList();
    NexusLog.debug('surveys: ${_active.length} eligible');
    if (_cfg.surveyAutoShow) _showNextEligible(null);
  } catch (e) {
    NexusLog.warn('surveys fetch failed: $e');
  }
  return _active;
}