firebase - 使用 firebase 的基于角色的 Flutter 应用程序
问题描述
我正在做一个名为事件管理颤振应用程序的大学项目。需要有基于角色的身份验证。
需要的不同角色是:
- 用户
- 学生协调员
- 活动协调员
- 主要的
- 霍德
- 实验室充电
我为不同的角色创建了布尔值。
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 的布尔值来获取数据和更新角色?
解决方案
我个人喜欢创建一个可访问的函数,可以调用它来更新给定用户的所有信息(如果你的文档很大,我不推荐这样做)。
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。
推荐阅读
- typescript - 用于访问父元素的“this”关键字
- r - R Shiny 直到几页刷新后才显示情节
- javascript - blob 到文本:将 blob 转换为 pdf 和 doc 文件的文本时无法读取文件内容
- akka - 在 Akka Typed 中测试行为平等
- django - uwsgi_pass 不转发 SCRIPT_NAME 标头
- python-3.x - Google-Colab 没有这样的文件或目录
- python - 在 C++ 和 python 之间共享数据,实现了 keras\tensorflow
- c++ - C++ 引用和瞬态对象
- javascript - 角度组件内的表单不提交
- html - 将 Gif 图像转换为 Base64,同时保留循环计数