Flutter and Firebase Account Deletion
Most of Flutter and Firebase Account Deletion have experienced at least once in our whole life that we downloaded an application because it looked too good in the advertising, but after signing up, it looks
othing like that. At that point, we want to delete the application and remove all.
The data we have provided to the app. That’s where account deletion is needed. Besides that, Apple
and Google now have strict policies that state that the apps that allow account creation should allow account deletion.
Pre-requisite Flutter and Firebase
- Flutter Project
- Firebase Setup in Flutter App
- Authentication Flow Setup
Adding Dependencies Flutter and Firebase
As for account deletion Flutter and Firebase, account creation is mandatory. You can read our guide and
create the authentication flow if you still need to write the code. You must add the following dependencies for this tutorial by running the below command. flutter pub add firebase_core firebase_auth get
Implementation Flutter and Firebase Account Deletion
We will start by creating the screen that shows the user a disclaimer and asks for a password for account deletion to ensure it is not accidentally deleted.
Delete Account Screen Code
Delete Account Screen Code
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../controllers/auth_controller.dart';
import '../../core/constants/color_constants.dart';
import '../../core/constants/path_constants.dart';
import '../../core/theme/text_theme.dart';
import '../../widgets/custom_button.dart';
class DeleteAccountScreen extends StatefulWidget {
const DeleteAccountScreen({super.key});
@override
State<DeleteAccountScreen> createState() => _DeleteAccountScreenState();
}
class _DeleteAccountScreenState extends State<DeleteAccountScreen> {
late TextEditingController passwordController;
@override
void initState() {
passwordController = TextEditingController();
super.initState();
}
@override
void dispose() {
passwordController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: IconButton(
onPressed: () {
Get.back();
},
color: ColorConstants.greyColor,
icon: const Icon(
Icons.arrow_back_ios_new_rounded,
),
),
title: Image.asset(
PathConstants.logo,
width: Get.width * 0.25,
),
),
body: Padding(
padding: EdgeInsets.symmetric(horizontal: Get.width * 0.06),
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
SizedBox(height: Get.height * 0.04),
const Text(
'Delete Account ',
style: AppTextTheme.bold18,
),
SizedBox(
height: Get.height * 0.012,
),
Text(
'''Are you sure you want to delete your account? Please read
the information below to understand how account deletion will affect you.
Deleting your account will remove your personal information from our
database. Your email address will be permanently reserved and cannot be
used to register a new account in the future.
Please enter your password to confirm the deletion of your account.''',
style: AppTextTheme.regular14.copyWith(
color: ColorConstants.greyColor,
),
),
SizedBox(
height: Get.height * 0.02,
),
Text(
'Password',
style: AppTextTheme.regular14.copyWith(
color: ColorConstants.greyColor,
),
),
TextField(
cursorColor: ColorConstants.redColor,
style: AppTextTheme.regular16,
controller: passwordController,
obscureText: true,
decoration: const InputDecoration(
hintText: '*******',
prefixIcon: Icon(Icons.lock_outline),
),
),
SizedBox(
height: Get.height * 0.04,
),
CustomButton(
color: ColorConstants.redColor,
onPressed: () {
Get.find<AuthController>()
.deleteAccount(passwordController.text);
},
width: Get.width,
height: Get.height * 0.06,
radius: 16,
child: const Text(
'Delete Account',
style: AppTextTheme.bold18,
),
),
],
),
),
),
);
}
}
In the above code Flutter and Firebase Account Deletion, we have added text as a disclaimer and the text field along with the controller that is initialized in the init method and disposed of in the dispose
method. A button below the field lets users delete the account after entering their
passwords. We have wrapped the whole column with single child scroll view to make
the screen scrollable when the keyboard pops up Flutter and Firebase.
Backend Code
Backend Code
Future<void> deleteAccount(String password) async {
Get.dialog(const Loader(), barrierDismissible: false);
try {
AuthCredential credential = EmailAuthProvider.credential(
email: FirebaseAuth.instance.currentUser!.email!,
password: password,
);
await FirebaseAuth.instance.currentUser!
.reauthenticateWithCredential(credential);
await FirebaseFirestore.instance
.collection(AppConstants.userCollection)
.doc(userModel.id)
.delete();
await FirebaseAuth.instance.currentUser!.delete();
Get.offAllNamed(NamedRoutes.walkthrough);
} catch (e) {
Get.back();
Get.snackbar(
'Error',
'Error while deleting account',
backgroundColor: ColorConstants.redColor,
colorText: ColorConstants.whiteColor,
);
}
}
Below is the detailed explanation of the delete account method:
- The method takes a single parameter, password, which is the password of the user who wants to delete their account.
- A loading dialog is shown using Get.dialog to indicate that a process is ongoing.
- An AuthCredential is created using the email of the currently signed-in user and the password provided as a parameter. This credential is used to reauthenticate the user before deleting their account.
- The reauthenticateWithCredential method of the FirebaseAuth instance is called with the created credential. This is to ensure that the user is who they claim to be before their account is deleted.
- If the reauthentication is successful, the user’s document in the Firestore collection is deleted using the delete method.
- The user’s account is then deleted from Firebase Authentication using the delete method in flutter forums.
- After the account is deleted, the user is redirected to the walkthrough screen using Get.offAllNamed.
- If any error occurs during this process, the loading dialog is closed using Get.back and a Snackbar is shown with an error message Flutter and Firebase.
The deleteAccount method is asynchronous, which means it returns a Future that completes when the account is deleted. This allows the method to perform I/O operations (like communicating with Firebase) without blocking the rest of the application.
Conclusion
In this tutorial, you’ve learned how to implement account deletion in a Flutter application using Firebase. The process involved creating a Delete Account screen that prompts the user to confirm their decision and enter their password for security reasons.
The backend code for account deletion was also implemented, which involved reauthenticating the user, deleting their document from Firestore, and finally deleting their account from Flutter and Firebase Account Deletion Authentication. This feature is important for maintaining user trust and is required by Apple and Google’s app store policies.