Flutter firebase: Delete from listview

95 Views Asked by At

I have an app with a collection 'users' and a subcollection 'Documents'. The 'users' has fields which includes 'Status'. The field 'Status' may have different values. The values include 'Not Verified', 'pending verification' and 'verified'. The admin access the subcollection 'Documents' and clicks on a button which changes the initial 'Status' field fo that a particular user from 'pending verification' to 'verified'. It works well. However, I want that after the admin changes the 'Status' field, that particular data should not be part of the the list anymore. In other words the list should contain only those users whose 'Status' has not been changed yet. I have no idea how to go about it. Any help?

code for accessing the subcollection 'Document':

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: () async {CollectionReference ref = 
FirebaseFirestore.instance.collection('users');
 final uid = snapshot.data!.docs[index]['UID'];
ref.doc(uid).set({'Verified'}, SetOptions(merge: true));))}})

So I want that after the click on the elevated button, and after the function is performed, that particular user should be deleted from the listview but not from the database.

Any help?

1

There are 1 best solutions below

0
On

I recommend that you extract the ListView.builder widget into a separate widget. This will allow you to use an array and update or delete elements from the array.

I hope it helps you. Regards. Btw, sorry if my English is not the best.

_PageContent(
        documents: snapshot.data!.docs,
      ),


class _PageContent extends StatefulWidget {
  final List documents;
  const _PageContent({
    Key? key,
    required this.documents,
  }) : super(key: key);

  @override
  State<_PageContent> createState() => _PageContentState();
}

class _PageContentState extends State<_PageContent> {
  List values = [];

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
      values = widget.documents;
    });
  }

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: widget.documents.length,
      itemBuilder: (context, index) {
        return GestureDetector(
          onTap: () {
            // This is like your elevatedButton OnPressed
            CollectionReference ref =
                FirebaseFirestore.instance.collection('users');
            final uid = snapshot.data!.docs[index]['UID'];
            //make your request ..... then...
        
            if (response is sucessfull) {
              values.removeAt(index);
              setState(() {});
            }
          },
          child: Card(),
        );
      },
    );
  }
}