0

I'm using shared preferences to store the current user's name, avatar, and a boolean for whether the user is logged in. Uninstalling the app seems to lead to a problem when re-installing. It appears related to shared preferences because when I log out and log back in the problem goes away. I suspect the shared preferences file is corrupted somehow and this triggers problems all over my app.

So is there a way I can either clear or delete the shared preferences file upon uninstall?

If it helps, here's the code for logging out;

Future<void> logOutSocial() async {
    try {
      socState();
      await socialLogin.logOutFacebook();
      await socialLogin.logOutGoogle();
      await socialLogin.logOutTwitter();

      currentname = "Anonymous";
      currentavatar = "https://example.com/default.jpg";
      currentlogged = false;
      currentuserid = "0";

      await savePreferences(
        currentname: "Anonymous",
        currentavatar: "https://example.com/default.jpg",
        currentlogged: false,
      );

      notifyListeners();
    } catch (e) {
      print(e);
    }
  }
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Meggy
  • 1,491
  • 3
  • 28
  • 63

2 Answers2

1

Android

There are two options:

  1. Disabling Auto Backup

beginning from API level 23 (Android 6) there Auto Backup is set to true by default. You need to edit AndroidManifest.xml file and set the boolean value of android:allowBackup. In your case it should be something similar to:

 <manifest ...>
        ...
        <application android:allowBackup = "false">
        </application>
    </manifest>

More info about AutoBackup

  1. Declare what you want to store

Everything is described quite clearly in the documentation.

iOS

Check this out.

Owczar
  • 2,335
  • 1
  • 17
  • 24
  • Are there any downsides to disabling allowBackup? What would I lose? – Meggy Sep 22 '20 at 12:33
  • You have everything in [documentation](https://developer.android.com/guide/topics/manifest/application-element#allowbackup). In a nutshell: if you disable this option, your app's data (but only those stored locally) could not be synced between devices which belong to the same Google account and user couldn't be able to recover app's local data after uninstalling. – Owczar Sep 22 '20 at 12:42
  • I just tried disabling it as you suggested, then uninstalled and re-installed a few times but the corrupted file is still there. – Meggy Sep 22 '20 at 12:45
  • Have you edited proper `AndroidManifest.xml`? It should be `YOUR_PACKAGE_NAME/android/app/src/main/AndroidManifest.xml`. Besides, all data of your app are stored in `/data/data/YOUR_PACKAGE_NAME/`, so make sure you haven't change your package name during development. You can also try to remove all data of your app manually, and then try to test everything with modified `AndroidManifest` file. – Owczar Sep 22 '20 at 13:08
  • 1
    As I'm not keen to do away with the allowBackups, so I directly slotted in Hive to replace shared preferences and it seems ok now. But thanks for your help it was useful to understand how it all works. – Meggy Sep 22 '20 at 14:17
0

Owczar's suggestion that I changed package name during development seemed to be my problem. But I decided to use Hive as a slot in replacement for Shared Preferences and that solved the issue as the Shared Preferences file is no longer neccessary.

Meggy
  • 1,491
  • 3
  • 28
  • 63
  • If you choosed to use Hive, you should keep in mind to use encrypted boxes to store sensitive data. Encryption key shold be stored in flutter_secure_storage or a similar package. Source: https://docs.hivedb.dev/#/advanced/encrypted_box – Owczar Sep 22 '20 at 15:40
  • Thanks for the heads up! – Meggy Sep 22 '20 at 15:56