showError method

void showError({
  1. required dynamic data,
  2. required BuildContext context,
})

to show API calling errors

Implementation

void showError({required var data, required BuildContext context}) {
  if (data.containsKey("detail")) {
    printMessage("---- ${data["detail"]}");
    if (data["detail"] == "Invalid token.") {
      Navigator.pushAndRemoveUntil(
          context,
          MaterialPageRoute(builder: (context) => const LoginScreen()),
          (route) => false);
    }
    showAlertDialog(
      alertMessage: data["detail"],
      context: context,
    );
  } else if (data.containsKey("non_field_errors")) {
    showAlertDialog(
      alertMessage: data["non_field_errors"][0],
      context: context,
    );
  } else if (data.containsKey("message")) {
    showAlertDialog(
      alertMessage: data["message"],
      context: context,
    );
  } else {
    showAlertDialog(
      alertMessage: "$data",
      context: context,
    );
  }
}