i have a flutter application where users have an email and password (accounts) and i want to give the user the option to delete their account if they want to, the problem is with my code sometimes only the authentication gets deleted and other times it delete the info and authentication (just like i want) so i dont know why sometimes it works and sometimes no

here is the code:

 onPressed: () async {
                        //delete user
                        current!.delete();
                        await FirebaseAuth.instance.signOut();
                        //delete user info in the database
                        var delete = await FirebaseFirestore.instance
                            .collection('users')
                            .doc(uid)
                            .delete();
                        //go to sign up log in page
                        await Navigator.pushNamed(context, '/');
                      },
                    )
                  ]).show();

thats some part of the code in this page, thank u.


Solution 1: Renaud Tarnec

You don't give any debugging details so it is difficult to help you.

However we can see that you sign out before calling the delete() method, so if the deletion of a user profile is only allowed to the authenticated user (via your security rules) you should encounter an error.

You should try to first call delete() and then sign out the user.


Solution 2: Ghala X

i found a solution!! here is the code that worked

onPressed: () async {
                        bool step1 = true ;
                        bool step2 = false ;
                        bool step3 = false ;
                        bool step4 = false ;
                        while(true){

                          if(step1){
                            //delete user info in the database
                              var delete = await FirebaseFirestore.instance
                                  .collection('users')
                                  .doc(uid)
                                  .delete();
                              step1 = false;
                              step2 = true;
                          }

                          if(step2){
                            //delete user
                            current!.delete();
                            step2 = false ;
                            step3 = true;
                          }

                          if(step3){
                            await FirebaseAuth.instance.signOut();
                            step3 = false;
                            step4 = true ;

                          }

                          if(step4){
                            //go to sign up log in page
                            await Navigator.pushNamed(context, '/');
                            step4 = false ;
                          }

                          if(!step1 && !step2 && !step3 && !step4 ) {
                            break;
                          }

                        }

                      },