英文:
flutter firebase: Admin update user status
问题
这有点困难,但如果有人帮助的话,我会很高兴。
我有一个与管理员面板连接的应用程序。
分解如下:
- 移动应用程序:
用户使用这些验证信息进行注册,数据存储在名为'users'的Firebase数据库集合中。
该集合包含字段'name'、'email'、'profile photo'、'status'等。
然后用户继续上传由政府提供的验证文档。此文档上传到名为'Document'的子集合中,这是'users'集合的子集合。它还包含与父集合'users'相同的字段以及一个额外的名为'document'的字段,用于上传的政府文档(这是在从集合中获取信息时方便访问的)。字段'status'默认为'未验证',将更改为'待验证'。一切运行得很完美。 - 管理员面板:
现在,在管理员面板中,管理员可以获取所有用户以及那些提交了他们的文档进行验证的用户。
现在,在从子集合'Document'中访问信息时,我希望在单击按钮时,主集合(users)中特定用户的字段'status'从'待验证'更新为'已验证'。
我该如何做?
这是管理员面板在屏幕上访问主集合'users'用户的方式。
QueryDocumentSnapshot<Map<String, dynamic>>? selectedUser;
StreamBuilder<QuerySnapshot<Map<String, dynamic>>>(
stream: FirebaseFirestore.instance.collection('users').snapshots(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data!.docs.length,
itemBuilder: (context, index) {
return Card(
child: ListTile(
onTap: () {
setState(() {
selectedUser = snapshot.data!.docs[index];
});
},
title: Text(
snapshot.data!.docs[index].get('Full name'),
),
),
);
},
);
}
if (snapshot.hasError) {
return const Text('Error');
} else {
return const Center(child: Text('Do requests yet'));
}
},
)
而这是管理员面板在不同屏幕上访问子集合'Document'的方式:
QueryDocumentSnapshot<Map<String, dynamic>>? pendingVerifications;
StreamBuilder<QuerySnapshot<Map<String, dynamic>>>(
stream: FirebaseFirestore.instance.collectionGroup('Documents').snapshots(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data!.docs.length,
itemBuilder: (context, index) {
return Card(
child: ListTile(
ElevatedButton(
onPressed: () {
// 在这里执行验证操作
},
child: const Text('Approve'),
),
),
);
},
);
}
},
)
这是您尝试在'onPressed'括号中编写的函数,但它没有起作用。因此,当管理员单击按钮时,它应该执行特定用户的操作:
CollectionReference ref = FirebaseFirestore.instance.collection('users');
ref.doc(selectedUser.id).update({'status': 'verified'});
这里使用了selectedUser.id
,这是您之前从'users'集合中选定的用户的文档ID。这将更新选定用户的'status'字段为'verified'。
英文:
This is kinda difficult but I would be glad if someone helps.
I have an app connected with admin panel.
The breakdown is like this:
- Mobile App:
The user signs up with those authentication staff and the data it's stored in firebase database in a collection called 'users'.
The collection contains the fields 'name', 'email', 'profile photo', 'status', etc.
The user then goes ahead to upload his/her government provided document for verification. This document is uploaded to database in a subcollection called 'Document'. This is a subcollection of the collection 'users'. It also contains the same field as the parent collection 'users' plus an additional field called 'document' for the uploaded government document(This is for easy access when getting info from the collection). The field 'status', which is by default 'Not verified' is changed to 'pending verification'. Everything works perfectly. - The admin panel:
Now in the admin panel, the admin can get all the users and also those users who have submitted their docs for verification.
Now in accessing the info from the subcollection 'Document', I want that at a click of a button the field 'status' of that particular user in the main collection (users) updates from 'pending verification' to 'verified'.
How do I do that?
This is how the admin panel accesses the main collection 'users' users on a screen.
QueryDocumentSnapshot<Map<String, dynamic>>? selectedUser;
StreamBuilder<QuerySnapshot<Map<String, dynamic>>>(
stream:
FirebaseFirestore.instance.collection('users').snapshots(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data!.docs.length,
itemBuilder: (context, index) {
return Card(
child: ListTile(
onTap: () {
setState(() {
selectedUser = snapshot.data!.docs[index];
});
},
title: Text(
snapshot.data!.docs[index].get('Full name'),
),
),
);
},
);
}
if (snapshot.hasError) {
return const Text('Error');
} else {
return const Center(child: Text('Do requests yet'));
}
},
),
And this is how the admin panel accesses the subcollection 'Document' on a different screen:
QueryDocumentSnapshot<Map<String, dynamic>>? pendingVerifications;
StreamBuilder<QuerySnapshot<Map<String, dynamic>>>(
stream: FirebaseFirestore.instance
.collectionGroup('Documants')
.snapshots(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data!.docs.length,
itemBuilder: (context, index) {
return Card(
child: ListTile(ElevatedButton( onPressed: () {}, child: const
Text('Approve')),
)
This is the function I tried to write in the 'onPressed' parenthesis but didn't work. (Thus, when the admin clicks on the button it should execute this of that particular user)
CollectionReference ref = FirebaseFirestore.instance.collection('users');
ref.doc().update({'Status': 'verification'});
user collection
Documents subcollection
Pleae help.
答案1
得分: 0
使用以下代码:
CollectionReference ref = FirebaseFirestore.instance.collection('users');
final uid = snapshot.data!.docs[index]['UID'];
ref.doc(uid).set({'Status': 'verification'}, SetOptions(merge: true));
重要的是要使用 set
与 SetOptions(merge: true)
,以确保不更改其他字段。
英文:
Try this:
CollectionReference ref = FirebaseFirestore.instance.collection('users');
final uid = snapshot.data!.docs[index]['UID'];
ref.doc(uid).set({'Status': 'verification'}, SetOptions(merge: true));
It is important to use set
with SetOptions(merge: true)
so that other fields are not changed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论