I’m working on a Flutter app and I have implemented account deletion functionality. The issue I’m facing is that after the account is deleted successfully
{“success”:1,”status”:203,”message”:”Your account has been deleted
successfully.”},
but the user remains logged in.
When I debugged the code I found that it is not entering in if
block. It comes here if (success) {..}
but doesn’t enter inside.
Here’s the relevant code snippet from my deleteUserAccount function:
Future<void> deleteUserAccount(
BuildContext context,
DeleteAccountProvider deleteAccountProvider,
UserProvider userProvider,
) async {
try {
final confirmed = await showDialog(
context: context,
builder: (context) {
return CupertinoAlertDialog(
title: Text(AppLocalizations.of(context)!.accountDeletion),
content: Text(AppLocalizations.of(context)!.sure),
actions: [
TextButton(
onPressed: () => Navigator.pop(context, false),
child: Text(AppLocalizations.of(context)!.cancel),
),
TextButton(
onPressed: () => Navigator.pop(context, true),
child: Text(AppLocalizations.of(context)!.delete),
),
],
);
});
if (confirmed != null && confirmed) {
SharedPreferences prefs = await SharedPreferences.getInstance();
String? userToken = prefs.getString('user_login_token');
final email = userProvider.user?.email;
final success = await deleteAccountProvider.deleteAccount(context, userToken ?? '', email ?? '');
if (success) {
await logout();
}
}
} catch (e) {
print('An error occurred: $e');
}
}
Future<void> logout() async {
final prefs = await SharedPreferences.getInstance();
if (kDebugMode) {
print('Language Code After Logout: ${prefs.getString('languageCode')}');
}
if (mounted) {
context.read<UserProvider>().clearUserData();
}
await prefs.remove("user_login_token");
if (mounted) {
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(
builder: (context) => const OnBoardingScreen(),
),
(route) => false);
}
}
Here is my deleteAccount class
Future<bool> deleteAccount(BuildContext context, String token, String email) async {
bool isConnected = await checkInternetConnectivity();
if (!isConnected) {
if (context.mounted) {
SnackBarUtils.showSnackBar(context, AppLocalizations.of(context)!.checkInternet);
}
return false;
}
try {
_isLoading = true;
notifyListeners();
var response = await ApiClass.delete(token, email);
if (response.statusCode == 200) {
final jsonResponse = json.decode(response.body);
final int success = jsonResponse["success"];
final int status = jsonResponse["status"];
if (success == 1 && status == 203) {
return true;
} else {
// Handle unsuccessful deletion
return false;
}
} else {
// Handle API error
return false;
}
} on SocketException catch (e) {
// The device is connected to a WiFi network that does not have internet access.
final errorMessage = 'Failed to connect to host server: $e';
if (context.mounted) {
SnackBarUtils.showSnackBar(context, errorMessage);
}
return false;
} finally {
_isLoading = false;
notifyListeners();
}
}