Home > Mobile >  How can I access the fields in firebase firestore?
How can I access the fields in firebase firestore?

Time:11-11

How can I access the status field ? I am working on ride sharing app. I just want that whenever I close the application after the login then next time I will redirect to the home screen.

I have two options login as driver or pessenger ,but whenever I loggedin as pessenger and closed the application, it will redirect me to the Driver home screen. I just want to access status field so that I can make a conditional difference between driver and pessenger I am pasting the code below, kindly check it and help me if you can!!

class SplashServices {
   void isLogin(BuildContext context) {
     final auth = FirebaseAuth.instance;
     final user = auth.currentUser;

     if (user != null) {
       Timer(
        const Duration(seconds: 3),
        () => Navigator.push(context,
          MaterialPageRoute(builder: (context) => const DriverPost())));
     } else {
         Timer(
          const Duration(seconds: 3),
          () => Navigator.push(context,
             MaterialPageRoute(builder: (context) => const GettingStarted())));
     }
  }

}

CodePudding user response:

I'm not sure I entirely understand the question, but with the way that your doc is set up with the status field, which I'm assuming will have two options, driver or passenger, you could access the value through a function like this or similar to it:

Future<void> checkUserStatus() {
  final driver = FirebaseAuth.instance.currentUser;
  final driverDoc = await FirebaseFirestore.instance.collection('driver').doc(driver!.uid).get();
  if (userDoc.data()!['status'] == 'driver'){
    Navigator.push(context,
          MaterialPageRoute(builder: (context) => const DriverPost()))
    
} else{
  Navigator.push(context,
             MaterialPageRoute(builder: (context) => const GettingStarted()))

}
}

Let me know if that helps.

  • Related