send static method

Future<String?> send(
  1. String urlWithoutParameters,
  2. Object data, {
  3. Map<String, String>? headers,
  4. int timeoutMS = 10000,
})

send Function used to send the data to the server. This is for post method. Returns the response body as a String in case of success and null otherwise. urlWithoutParameters The matomo.php URL without the parameters. data The parameters to be sent by post. headers The headers to be sent along with request.

Implementation

static Future<String?> send(
  String urlWithoutParameters,
  Object data, {
  Map<String, String>? headers,
  int timeoutMS = 10000,
}) async {
  try {
    final res = await http
        .post(
          Uri.parse(urlWithoutParameters),
          body: data,
          headers: headers,
        )
        .timeout(Duration(
          milliseconds: timeoutMS,
        ));
    if (res.statusCode.toString()[0] != "2") {
      if (res.body.startsWith("{\"error\":")) {
        return (res.body);
      }
      return ('{"error":"code ${res.statusCode}",'
          '"message":'
          '"${res.body.replaceAll('"', "'").replaceAll("\n", "")}"}');
    }
    return (res.body);
  } catch (e, st) {
    debugPrint("$e $st");
    return (null);
  }
}