Home > Net >  Not redirecting on desired screen
Not redirecting on desired screen

Time:10-17

I want, if a user either its a doc or patient when they sign in they are sent to their respective screen but the code that I wrote is not working!

signInsignUpButton(context, true, () async {
                  await FirebaseAuth.instance
                      .signInWithEmailAndPassword(
                    email: _emailTextController.text,
                    password: _passwordTextController.text,
                  )
                      .then((value) async {
                    // print(value);

                    final userid =
                        FirebaseAuth.instance.currentUser!.uid.toString();
                    print(userid);

                    var doctors = await FirebaseFirestore.instance
                        .collection('Doctors')
                        .where('D_Id', isEqualTo: userid)
                        .get();

                    var patients = await FirebaseFirestore.instance
                        .collection('Patients')
                        .where('uid', isEqualTo: userid)
                        .get();

                    var admins = await FirebaseFirestore.instance
                        .collection('Admins')
                        .where('uid', isEqualTo: userid)
                        .get();

                    if (patients != null) {
                      Navigator.push(
                          context,
                          MaterialPageRoute(
                              builder: (context) => HomeScreen()));
                    } else if (doctors != null) {
                      Navigator.push(
                          context,
                          MaterialPageRoute(
                              builder: (context) => HomeScreenDoc()));
                    } else if (admins != null) {
                      Navigator.push(context,
                          MaterialPageRoute(builder: (context) => MainHome()));}  

issue is that it only check first if statement and ignores the rest and redirects to the screen in first if statement, its not checking if uid is of this collection or not!

CodePudding user response:

Solution:

   if (patients.docs.isNotEmpty) {
                  Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => HomeScreen()));
                } else if (doctors.docs.isNotEmpty) {
                  Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => HomeScreenDoc()));
                } else if (admins.docs.isNotEmpty) {
                  Navigator.push(context,
                      MaterialPageRoute(builder: (context) => MainHome()))

The Reason for this is because admins will not be null if there is or isnt any results. the .get() function will return u a QuerySnapshot.

var admins = await FirebaseFirestore.instance
                    .collection('Admins')
                    .where('uid', isEqualTo: userid)
                    .get();

QuerySnapshot has a attribute docs which returns an array of documents. .isNotEmpty will return true if an array is not empty.

optionally, u could also use .size

if (patients.size > 0){
}

https://pub.dev/documentation/cloud_firestore/latest/cloud_firestore/QuerySnapshot-class.html

  • Related