post method

Future<void> post(
  1. String endpoint,
  2. Map<String, String> headers,
  3. Map<String, dynamic>? body
)

Implementation

Future<void> post(String endpoint, Map<String, String> headers,
    Map<String, dynamic>? body) async {
  final Uri uri = Uri.parse('$url$endpoint');

  try {
    final http.Response res = await (body!.isNotEmpty
        ? http.post(
            uri,
            headers: headers,
            body: jsonEncode(body),
          )
        : http.post(
            uri,
            headers: headers,
          ));

    response = res.body;
    httpCode = res.statusCode;

    final Map<String, dynamic> jsonResponse = jsonDecode(response!);

    errorMessage = jsonResponse['error']?['message'];
    messageId = jsonResponse['messages']?[0]?['id'];
    phoneNumber = jsonResponse['contacts']?[0]?['input'];

    if (res.statusCode >= 400) {
      error = res.reasonPhrase;
    }
  } catch (e) {
    error = e.toString();
    // throw ('An error occurred: $error');
  }
}