Closed. This question needs to be more
focused。它当前不接受答案。
想改善这个问题吗?更新问题,使其仅关注
editing this post一个问题。
上个月关闭。
Improve this question
我正在从事一个名为事件管理 flutter 应用程序的大学项目。需要具有基于角色的身份验证。
需要的不同角色是:
用户学生协调员 Activity 协调员校长 HOD LabIncharge 我为不同的角色创建了 bool(boolean) 值。
bool admin = false;
bool eCoordinator = false;
bool labIncharge = false;
bool principal = false;
bool hod = false;
bool sCoordinator = false;
用户数据将具有指定角色的字段:
usersReference.document(gCurrentUser.id).setData({
"id": gCurrentUser.id,
"profileName": gCurrentUser.displayName,
"username": username,
"url": gCurrentUser.photoUrl,
"email": gCurrentUser.email,
"bio": "",
"timestamp": timestamp,
"admin": admin,
"hod": hod,
"labIncharge": labIncharge,
"principal": principal,
"eCoordinator": eCoordinator,
"sCoordinator": sCoordinator,
});
在Cloud Firestore中的外观:
如何通过更改Firebase中的 bool(boolean) 值来获取数据和更新角色?
我个人喜欢创建一个可访问的函数,可以调用该函数来更新给定用户的所有信息(但是,如果您的文档很大,我不建议这样做)。
Future<void> updateUser(User userData) async {
// CollectionReference get _usersCollection => _firestore.collection('USERS');
DocumentReference docRef =
_usersCollection.document(userData.firebaseUser.uid);
Map<String, dynamic> userJson = userData.toJson();
await docRef.setData(userJson, merge: true);
}
}
您的用例可能会根据您的类结构和使用方式而有所变化,但是此解决方案的核心是:
await docRef.setData(userJson, merge: true);
您可以在其中将任何更改的数据写入Firestore。