submit method

Future<String> submit(
  1. Map<String, dynamic> payload
)

Implementation

Future<String> submit(Map<String, dynamic> payload) async {
  if (!_active) throw StateError('SDK disabled');
  if (_requiresDeviceApproval) await deviceRegistration.refresh();
  if (!isDeviceApproved) {
    throw StateError(
      'This device needs approval in the web console before sending feedback.',
    );
  }
  final client = http.Client();
  _pendingClient = client;
  try {
    final response = await client
        .post(
          config.endpoint.resolve('/v1/reports'),
          headers: {
            'Authorization': 'Bearer ${config.apiKey}',
            if (deviceRegistration.credential != null)
              'X-Reporter-Device': deviceRegistration.credential!,
            'Content-Type': 'application/json',
          },
          body: jsonEncode(payload),
        )
        .timeout(const Duration(seconds: 30));
    if (response.statusCode == 403 && _requiresDeviceApproval) {
      deviceRegistration.invalidate();
    }
    if (response.statusCode != 200 && response.statusCode != 201) {
      throw StateError(
        'Submission failed (${response.statusCode}): ${response.body}',
      );
    }
    return (jsonDecode(response.body) as Map)['id'] as String;
  } finally {
    client.close();
    _pendingClient = null;
  }
}