Whenever I use FocusManager or FocusScope.of(context) my code breaks and the controller.loading values are ignored.
When the keyboard is not open the code works perfectly fine
and
even if I place the FocusManager After the await it works perfectly fine
But
The Focus Manager or FocusScopr.of(context) break when placed like below
void resetPassword() async {
FocusManager.instance.primaryFocus?.unfocus(); // Placed Here the code behaves very diffrently
try {
FocusManager.instance.primaryFocus?.unfocus(); // Also breaks here or at any place behind the await function
controller.changeErrorStatus(false);
if (emailController.text.isEmpty) {
controller.changeErrorStatus(true);
controller.changeErrorMessage(
"An Error Occurred, Email Field cannot be left blank.");
controller.startLoading(false);
return;
}
controller.startLoading(true);
await Future.delayed(Duration(seconds: 5));
// await FirebaseAuth.instance
// .sendPasswordResetEmail(email: emailController.text.trim())
// .timeout(const Duration(seconds: 5));
controller.startLoading(false);
} catch (e) {
controller.changeErrorStatus(true);
controller.startLoading(false);
controller.changeErrorMessage("An Error Occurred, $e");
}
}
It Works Like this
try {
controller.changeErrorStatus(false);
if (emailController.text.isEmpty) {
controller.changeErrorStatus(true);
controller.changeErrorMessage(
"An Error Occurred, Email Field cannot be left blank.");
controller.startLoading(false);
return;
}
controller.startLoading(true);
await Future.delayed(Duration(seconds: 5));
FocusManager.instance.primaryFocus?.unfocus(); // If Focus Scope is here everything works Fine
// await FirebaseAuth.instance
// .sendPasswordResetEmail(email: emailController.text.trim())
// .timeout(const Duration(seconds: 5));
controller.startLoading(false);
Also tried calling it from the onTap function and it still breaks
onTap:(){
FocusManager.instance.primaryFocus?.unfocus();
resetPassword();
}
The controller.isLoading values are right but the button disregards then for I don’t what reason
Obx(
() => Container(
alignment: Alignment.center,
width: size.width * 0.8,
height: 50,
decoration: BoxDecoration(
color: mainController.isLoading.value
? AppColors().secHalfGrey
: AppColors().primaryBlue,
borderRadius: BorderRadius.circular(10)),
child: mainController.isLoading.value
? Lottie.asset("assets/jsons/atom-loader.json")
: Text(
buttonText,
style: context.textTheme.displayMedium,
),
),
),
2
Answers
I was messing around and found the answer
The Function is
I hope it helps anyone who is facing the same problem.
Use this :
Instead of :