在 Flutter 中,当用户登录时如何使用 firestore 将用户定向到各自的屏幕?

问题描述 投票:0回答:1

我在我的应用程序中使用 Flutter 中的 firestore 开发了一个注册系统,可以使用以下方法从数据库中检索用户数据:

//get user's data from firestore


 Future<String?> getUser(String email) async {
    try {
      CollectionReference users = FirebaseFirestore.instance.collection('users');
      final snapshot = await users.doc(email).get();
      final data = snapshot.data() as Map<String, dynamic>;
      return data['person_identity'];
    } catch (e) {
      return 'Error fetching user';
    }
  }

现在我想要什么?当用户登录时,根据他们的身份,我想将他们引导到他们的页面,就像他们是学生一样,将他们带到StudentScreen,否则带到TeacherScreen

如何做到这一点?

flutter firebase google-cloud-firestore firebase-realtime-database
1个回答
0
投票

在您的 firebase Collection --> 每个用户的文档中,我假设您有一个名为类型或角色的字段(例如名称:“luke”,personal_identity:Student)。

获取用户数据时,检查personal_identity(用户)是学生还是教师。 然后根据您的申请路线进行导航。

全局变量 细绳?用户类型 = "";

//get user data and store the personal Identity to the variable userType.
Future<void> getUser(String email) async {
try {
  CollectionReference users = FirebaseFirestore.instance.collection('users');
  final snapshot = await users.doc(email).get();
  final data = snapshot.data() as Map<String, dynamic>;
  userType = data['person_identity']
} catch (e) {
  return 'Error fetching user';
}

}

获取数据并将类型存储在变量 userTypeInternal 中

Future<void> signIn(String userTypeFromFIrebase) async {
try {
  var navContext = Navigator.of(context);

  // Check if user exists
  bool userExists = false;
  final userTypeInternal = userTypeFromFIrebase ;
  UserCredential userCredential =
      await FirebaseAuth.instance.signInWithEmailAndPassword(
    email: email.toLowerCase().trim(),
    password: password.trim(),
  );
  userExists = userCredential.user != null;

  // Navigate to the home screen of choice if user exists
  if (userExists = userCredential.user != null && 
     userTypeInternal=="Student") {
    //this is where you implement moving to the screen
    navContext.pushNamedAndRemoveUntil('/studentScreen', (route) => 
false);
  } else if (userExists = userCredential.user != null && 
     userTypeInternal=="Teacher") {
    //this is where you implement moving to the screen
    navContext.pushNamedAndRemoveUntil('/TeacherScreen', (route) => 
false);
  }
else if (!userExists) {
    Fluttertoast.showToast(
        backgroundColor: Colors.green,
        msg: "email doesn't exist",
        textColor: Colors.white);
  } else {
    Fluttertoast.showToast(
        backgroundColor: Colors.green,
        msg: "Email account not registered",
        textColor: Colors.white);
  }
} on FirebaseAuthException catch (e) {
  if (e.code != 'user-not-found') {
  //flutter toast message
    Reuse.callSnack(
      context,
      "Email not registered",
    );
  
    return;
  } else if (e.code != 'wrong-password') {
    Reuse.callSnack(context, "Wrong password");
  
} catch (error) {
  // Handle other types of errors
  switch (error) {
    case 'ERROR_INVALID_EMAIL':
    case 'ERROR_WRONG_PASSWORD':
    case 'ERROR_USER_DISABLED':
    case 'ERROR_TOO_MANY_REQUESTS':
    case 'ERROR_OPERATION_NOT_ALLOWED':
      Reuse.callSnack(context, error.toString());
      break;
    default:
      Reuse.callSnack(context, 'An unknown error occurred');

      break;
  }
}

}

© www.soinside.com 2019 - 2024. All rights reserved.