updateAddress method

void updateAddress(
  1. String addressId,
  2. String updatedAddress,
  3. UpdateAddress updateAddress
)

Implementation

void updateAddress(String addressId, String updatedAddress,
    UpdateAddress updateAddress) async {
  updateAddress.onLoading();

  final url = "https://api.plentrasphere.com/v2/client/index.php";
  final body = {
    'class': 'address',
    "appKey": appKey,
    "action": "updateAddress",
    "addressId": addressId,
    "address": updatedAddress,
  };

  try {
    final response = await http.post(
      Uri.parse(url),
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
        'Authorization': 'Bearer $token'
      },
      body: body,
    );

    final data = json.decode(response.body);
    int code = data['response']['code'];
    String status = data['response']['status'];

    if (code == 400) {
      if (status == "session-expired" || status == "please-login") {
        updateAddress.onNotLoggedIn();
        updateAddress.onLoadfinished();
        return;
      }
      if (status == "app-expired") {
        updateAddress.onAppNotActive(data['info']['appName']);
        updateAddress.onLoadfinished();
        return;
      }

      updateAddress.onError(status);
      updateAddress.onLoadfinished();
      return;
    }

    updateAddress.onSuccess(status);
    updateAddress.onLoadfinished();
  } catch (e) {
    updateAddress.onError(e.toString());
    updateAddress.onLoadfinished();
  }
}