updatePhoneNumber method
Implementation
Future<bool> updatePhoneNumber(String phoneNo) async {
authenticationController.resultPin.value = '';
final Completer<bool> completer = Completer<bool>();
try {
loadingDialog();
await FirebaseAuth.instance.verifyPhoneNumber(
phoneNumber: phoneNo,
forceResendingToken: myResendToken,
verificationCompleted: (PhoneAuthCredential credential) async {
if (kDebugMode) {
print("print called from verification completed ");
}
try {
await FirebaseAuth.instance.currentUser
?.updatePhoneNumber(credential);
CustomFlashWidget.showFlashMessage(
title: "Success",
message: "Phone number updated successfully!",
type: FlashType.success,
);
completer.complete(true); // Resolve Completer with true
} catch (e) {
if (kDebugMode) {
print("Error updating phone number: $e");
}
CustomFlashWidget.showFlashMessage(
title: "Error",
message: e.toString(),
);
completer.complete(false);
}
},
verificationFailed: (FirebaseAuthException e) {
closeDialog();
if (e.code == 'invalid-verification-code') {
CustomFlashWidget.showFlashMessage(
type: FlashType.error,
title: "Invalid Code",
message:
"The verification code you entered is incorrect. Please try again.",
);
} else if (e.code == 'session-expired') {
CustomFlashWidget.showFlashMessage(
type: FlashType.error,
title: "Session Expired",
message:
"The verification code has expired. Request a new code and try again.",
);
} else if (e.code == 'invalid-phone-number') {
CustomFlashWidget.showFlashMessage(
type: FlashType.error,
title: "Invalid Phone Number",
message:
"The phone number is not valid. Please enter a correct phone number with the country code.",
);
} else if (e.code == 'quota-exceeded') {
CustomFlashWidget.showFlashMessage(
type: FlashType.error,
title: "Quota Exceeded",
message:
"SMS verification limit has been reached. Try again later.",
);
} else {
CustomFlashWidget.showFlashMessage(
type: FlashType.error,
title: "Error",
message: e.message ??
"An unexpected error occurred. Please try again.",
);
}
completer.complete(false);
},
codeSent: (String verificationId, int? resendToken) async {
closeDialog();
CustomFlashWidget.showFlashMessage(
title: "Success",
message: "OTP has been resent successfully!",
type: FlashType.success,
);
myResendToken = resendToken;
myVerificationID = verificationId;
authenticationController.startTimer();
await OtpVerificationView.show(
barrierDismissible: false,
onConfirm: () {
closeDialog();
},
onResend: () {
resendOTP(phoneNo);
},
);
if (authenticationController.resultPin.value.isNotEmpty) {
PhoneAuthCredential credential = PhoneAuthProvider.credential(
verificationId: verificationId,
smsCode: authenticationController.resultPin.value,
);
try {
await FirebaseAuth.instance.currentUser
?.updatePhoneNumber(credential);
CustomFlashWidget.showFlashMessage(
title: "Success",
message: "Phone number updated successfully!",
type: FlashType.success,
);
completer.complete(true);
} catch (e) {
if (kDebugMode) {
print("Error updating phone number: $e");
}
CustomFlashWidget.showFlashMessage(
title: "Error",
message: e.toString(),
);
completer.complete(false);
}
} else {
completer.complete(false);
}
},
codeAutoRetrievalTimeout: (String verificationId) {
closeDialog();
if (kDebugMode) {
print("print called from codeAutoRetrievalTimeout ");
}
},
);
} catch (e) {
if (kDebugMode) {
print("Error in updatePhoneNumber: $e");
}
closeDialog();
CustomFlashWidget.showFlashMessage(title: "Error", message: e.toString());
completer.complete(false);
}
return completer.future;
}