比较从 Firestore 检索的数据的条件
Compare the condition from the data retrieve from Firestore
我想设置条件以在每次登录应用程序时验证 firestore 中的用户类型。
例如:
如果用户类型是 Customer 我想去 CustomerMainScreen ;
如果用户类型是坐月子我想去 CLLadyMainScreen;
这是我的 Firestore 图像:
Firestore
下面是我的代码:
Widget build(BuildContext context) {
return StreamBuilder(
stream: FirebaseAuth.instance.authStateChanges(),
builder: (context, userSnapshot) {
// if (userSnapshot.hasData && userSnapshot.data['userType']== 'Confinement Lady') {
// return CLLadyMainScreen();
// }
// if (userSnapshot.hasData && userSnapshot.data['userType']== 'Customer') {
// return CustomerMainScreen();
// }
if (userSnapshot.hasData) {
if (userSnapshot.data()['userType'] == 'Confinement Lady') {
return CLLadyMainScreen();
} else if (userSnapshot.data()['userType'] == 'Customer') {
return CustomerMainScreen();
}
}
return AuthScreen();
});
}
但是当我点击登录按钮时弹出这个错误:
Error message
我应该如何解决这个问题?
目前存在的问题:
Problem
首先您需要另一个Streambuilder 来访问Firestore 中的文档,并将另一个流传递给当前用户的信息。我还假设集合中的每个用户文档都是用户的 UID:
if(userSnapshot.hasData) {
return StreamBuilder(
stream: FirebaseFirestore.instance.collection('users').doc(FirebaseAuth.instance.currentUser?.uid).snapshots(),
builder: (context, snapshot) {
// snapshot returns user doc.
if(snapshot.data == null) {
return Container(); // Return some kind of spinner or loading widget so firebase can retrieve the data and you don't get an error
}
if (snapshot.data['userType'] == 'Confinement Lady') {
return CLLadyMainScreen();
} else {
return CustomerMainScreen();
}
}
} else {
return AuthScreen();
}
我想设置条件以在每次登录应用程序时验证 firestore 中的用户类型。
例如:
如果用户类型是 Customer 我想去 CustomerMainScreen ;
如果用户类型是坐月子我想去 CLLadyMainScreen;
这是我的 Firestore 图像: Firestore
下面是我的代码:
Widget build(BuildContext context) {
return StreamBuilder(
stream: FirebaseAuth.instance.authStateChanges(),
builder: (context, userSnapshot) {
// if (userSnapshot.hasData && userSnapshot.data['userType']== 'Confinement Lady') {
// return CLLadyMainScreen();
// }
// if (userSnapshot.hasData && userSnapshot.data['userType']== 'Customer') {
// return CustomerMainScreen();
// }
if (userSnapshot.hasData) {
if (userSnapshot.data()['userType'] == 'Confinement Lady') {
return CLLadyMainScreen();
} else if (userSnapshot.data()['userType'] == 'Customer') {
return CustomerMainScreen();
}
}
return AuthScreen();
});
}
但是当我点击登录按钮时弹出这个错误: Error message
我应该如何解决这个问题?
目前存在的问题: Problem
首先您需要另一个Streambuilder 来访问Firestore 中的文档,并将另一个流传递给当前用户的信息。我还假设集合中的每个用户文档都是用户的 UID:
if(userSnapshot.hasData) {
return StreamBuilder(
stream: FirebaseFirestore.instance.collection('users').doc(FirebaseAuth.instance.currentUser?.uid).snapshots(),
builder: (context, snapshot) {
// snapshot returns user doc.
if(snapshot.data == null) {
return Container(); // Return some kind of spinner or loading widget so firebase can retrieve the data and you don't get an error
}
if (snapshot.data['userType'] == 'Confinement Lady') {
return CLLadyMainScreen();
} else {
return CustomerMainScreen();
}
}
} else {
return AuthScreen();
}