createCustomer method

void createCustomer(
  1. BuildContext context
)

Implementation

void createCustomer(BuildContext context) async {
  FocusScope.of(context).requestFocus(FocusNode());
  if (formKey.currentState!.validate()) {
    FocusScope.of(context).unfocus();
    try {
      isLoading(true);
      var request = {
        'merchant_id': Storage.getValue(Constants.merchantID),
        'api_key': await Constants.apiKey(),
        'sub_client_api_key': Storage.getValue(Constants.subClientApiKey),
        'email': email.text.toString(),
        "first_name": firstName.text.toString(),
        "last_name": lastName.text.toString(),
        "gender": gender.toString(),
        "phone_number": phoneNumber.text.toString(),
        "meta": {"address": address.text.toString()},
      };
      var response = await DioClient().request(
          context: context,
          api: '/merchant/customers/create',
          method: Method.POST,
          params: request);
      CreateCustomerResponse createCustomerResponse =
          CreateCustomerResponse.fromJson(response);
      if (createCustomerResponse.status == Strings.success) {
        Storage.removeValue(Constants.customerID);
        Storage.saveValue(
            Constants.customerID, createCustomerResponse.data!.id.toString());
        // Utils.navigationPush(
        // context,
        // Shops(
        //   initialScreen: initialScreen,
        //   merchantId: Storage.getValue(Constants.merchantID),
        // ));
        Utils.navigationReplace(
            context,
            SuccessfulMgs(
              successTitle: Strings.customerCreatedSuccessfully,
              successMessage: DateFormat.jm().format(
                  DateTime.parse(DateTime.now().toString()).toLocal()),
            ));
        allCustomersController.fetchCustomers(context);
      } else {
        return Utils.showSnackbar(context, Strings.error,
            response['message'].toString().toTitleCase(), AppColors.red);
      }
    } catch (e) {
      return Utils.showSnackbar(
          context, Strings.error, e.toString(), AppColors.red);
    } finally {
      isLoading(false);
    }
    update();
  }
}