void registerGuardianAndSenior(String textual content) async {
// Present loading circle
showDialog(
context: context,
builder: (context) => const Heart(
youngster: CircularProgressIndicator(),
),
);
// Be sure that pw match
if (passwordController.textual content != comfirmPswdController.textual content) {
if (context.mounted) {
Navigator.pop(context);
displayMessageToUser("Passwords do not match!", context);
}
} else {
strive {
if (textual content == "Guardian") {
UserCredential? userCredential `your textual content`= await FirebaseAuth.occasion
.createUserWithEmailAndPassword(
e mail: emailController.textual content,
password: passwordController.textual content);
createUserDocument(userCredential);
} else {
FirebaseAuth auth = FirebaseAuth.occasion;
Consumer? currentUser = auth.currentUser;
if (currentUser != null) {
String currentUserEmail = currentUser.e mail!;
String currentUserName = usernameController.textual content;
String currentUserPhone = phoneNumController.textual content;
String currentUserGender = genderDropdownValue!;
// Signal out the present person
// await FirebaseAuth.occasion.signOut();
// Create the senior doc
await createSeniorDocument(
emailController.textual content,
currentUserEmail,
currentUserName,
currentUserPhone,
currentUserGender,
);
FirebaseApp secondaryApp = await Firebase.initializeApp(
identify: 'SecondaryApp',
choices: Firebase.app().choices,
);
strive {
UserCredential credential =
await FirebaseAuth.instanceFor(app: secondaryApp)
.createUserWithEmailAndPassword(
e mail: currentUserEmail,
password: passwordController.textual content,
);
} catch (e) {
print('An error occured. Please strive once more.');
}
// after creating the account, delete the secondary app as beneath:
await secondaryApp.delete();
}
}
if (context.mounted) {
Navigator.pop(context);
}
} on FirebaseAuthException catch (e) {
if (context.mounted) {
// Pop loading circle
Navigator.pop(context);
// ignore: use_build_context_synchronously
displayMessageToUser(e.code, context);
}
}
}
// Attempt creating the person
}
Future<void> createUserDocument(UserCredential? userCredential) async {
if (userCredential != null && userCredential.person != null) {
await FirebaseFirestore.occasion
.assortment("Customers")
.doc(userCredential.person!.e mail)
.set({
'e mail': userCredential.person!.e mail,
'username': usernameController.textual content,
'cellphone quantity': phoneNumController.textual content,
'gender': genderDropdownValue,
// 'seniorUID': null, // Use null as an alternative of Null
// 'character': characterDropdownValue,
});
}
}
Future<void> createSeniorDocument(
String seniorEmail,
String guardianEmail,
String userName,
String cellphone,
String gender,
) async {
strive {
// Create the senior's doc
await FirebaseFirestore.occasion
.assortment("SeniorCitizens")
.doc(seniorEmail)
.set({
'e mail': seniorEmail,
'username': userName,
'cellphone quantity': cellphone,
'gender': gender,
'guardianID': guardianEmail,
});
await FirebaseAuth.occasion.createUserWithEmailAndPassword(
e mail: seniorEmail, password: passwordController.textual content);
} catch (e) {
print("***Error creating senior doc: $e");
}
}`
I’m utilizing flutter and firebase for this present app
and hope that my present admin account can hold sign up after creating a brand new account for different members. The difficulty I’m dealing with is the brand new person will mechanically sign up and substitute the admin account I signed in earlier than. Your assist and advises will probably be appreciated thanks to your feedback.