I have already confirmed a certain user (with email and password) is registered in Firebase Authentication and I'm gonna try to check if try-catch errors works when users post duplicate email address, accordingly wrote codes as follows:
I expect with first tap like follows:
fail
would be printed on console at//here2
- and
AsyncData<bool>(value: false)
should be returned at//here3
- and finally user could be stayed as
navigatorAnimation()
does not work and just returned at//here4
However, It actually works with first tap like follows:
success
is printed on console at//here1
- and
AsyncLoading<bool>()
is returned at//here3
fail
is printed on console at//here3
I could not understand why these things happen with first single tap, especially, (try)success
and (catch)fail
are executed at the same time. And I could not find a lot about AsyncLoading<bool>()
in official revier_pod doc as well.
Additionally, I tried second tap and found it returned AsyncData<bool>(value: false)
as expected but fail
was not printed on console at //here3
.
I finally could not resolve this curious problem by myself and please kindly advise me what's the problem and how I can fix it as I expected.
//auth.dart
import 'package:firebase_auth/firebase_auth.dart';
Future<bool> authUser(ref) async {
try {
print("success");//here1
final FirebaseAuth auth = FirebaseAuth.instance;
final UserCredential _result =
await auth.createUserWithEmailAndPassword(
email: emailController.text,
password: maskController.text,
);
User _user = _result.user!;
_user.sendEmailVerification();
return true;
} on FirebaseAuthException catch (e) {
print("fail");//here2
if (e.code == 'email-already-in-use') {
ref.read(completeErrorMessage.notifier).update((state) => awesomeText(icon: FontAwesomeIcons.lightbulb, text: 'EZ01',color: MyStyle.alertColor));
} else if (e.code == 'invalid-email') {
ref.read(completeErrorMessage.notifier).update((state) => awesomeText(icon: FontAwesomeIcons.lightbulb, text: 'EZ02',color: MyStyle.alertColor));
} else if (e.code == 'operation-not-allowed') {
ref.read(completeErrorMessage.notifier).update((state) => awesomeText(icon: FontAwesomeIcons.lightbulb, text: 'EZ03',color: MyStyle.alertColor));
} else if (e.code == 'weak-password') {
ref.read(completeErrorMessage.notifier).update((state) => awesomeText(icon: FontAwesomeIcons.lightbulb, text: 'EZ04',color: MyStyle.alertColor));
} else if (e.code == 'network-request-failed') {
ref.read(completeErrorMessage.notifier).update((state) => awesomeText(icon: FontAwesomeIcons.lightbulb, text: 'EZ05',color: MyStyle.alertColor));
} else if (e.code == 'too-many-requests') {
ref.read(completeErrorMessage.notifier).update((state) => awesomeText(icon: FontAwesomeIcons.lightbulb, text: 'EZ06',color: MyStyle.alertColor));
}else{
ref.read(completeErrorMessage.notifier).update((state) => awesomeText(icon: FontAwesomeIcons.lightbulb, text: 'EZ10',color: MyStyle.alertColor));
}
return false;
}
}
//navigator.dart
import 'dart:developer';
import 'dart:ui';
import 'package:cards/control/auth.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
final authFutureProvider = FutureProvider<bool>(
(ref) async => authUser(ref),
);
class MyNavigator extends ConsumerWidget {
final Widget destinationTo;
final BuildContext context;
final String goBack;
final List<GlobalKey<FormState>>? formKeyList;
MyNavigator(
{required this.destinationTo,
required this.context,
required this.goBack,
this.formKeyList,
Key? key,
required})
: super(key: key) {
// print(destinationTo.runtimeType);
}
@override
Widget build(BuildContext context, WidgetRef ref) {
// }
return TextButton(
style: TextButton.styleFrom(
foregroundColor: MyStyle.mainColor,
),
onPressed: () {
switch (goBack) {
case "XX":
//omitted
break;
case "YY":
//omitted
break;
case "sendEmail":
ref.read(sendEmailPressProvider.notifier).update((state) => true);
if (ref.watch(sendEmailPressProvider)) {
final authC = ref.watch(authFutureProvider);
print(authC);//here3
authC.when(
data: (data) {
if (data == true) {
navigatorAnimation(
context: context,
goBack: goBack,
destinationTo: destinationTo);
} else {
return;//here 4
}
},
error: (error, stackTrace) => print(error),
loading: () => null,
);
}
break;
}
},
child: Text(goBack),
);
}
}
void navigatorAnimation(
{required BuildContext context,
required String goBack,
required Widget destinationTo}) {
Offset? begin;
Navigator.of(context).push(
PageRouteBuilder(
pageBuilder: (context, animation, secondaryAnimation) {
return destinationTo;
},
transitionDuration: const Duration(milliseconds: 300),
transitionsBuilder: (context, animation, secondaryAnimation, child) {
goBack == "XX" ? begin = const Offset(1.0, 0.0) : 0;
goBack == "YY" ? begin = const Offset(-1.0, 0.0) : 0;
goBack == "sendEmail" ? begin = const Offset(1.0, 0.0) : 0;
const Offset end = Offset.zero;
final Animatable<Offset> tween = Tween(begin: begin, end: end)
.chain(CurveTween(curve: Curves.easeInOut));
final Animation<Offset> offsetAnimation = animation.drive(tween);
return SlideTransition(
position: offsetAnimation,
child: child,
);
},
),
);
}