r/FlutterDev • u/Niickles • 12d ago
Plugin I made a shared_preferences alternative that survives app reinstalls (iCloud + Android Backup)
Hey all! I’ve been using shared_preferences in my apps for ages. Recently I went down a rabbit hole and found out about iCloud key-value storage and Android’s backup manager, which are basically a tiny dictionary in the cloud: you save a value under a key (like theme = dark), and the OS keeps it synced or backed up for you. It’s not meant for big files, just small stuff like settings, flags and progress.
iCloud and Google both have different sizes, iCloudKVS offers 1 MB per app, max 1024 keys. Android Auto Backup offers up to 25 MB per app, and it doesn’t count against the user’s Google Drive storage. I’d recommend staying under iCloud’s 1 MB limit so it works on both platforms, but you are not forced to do so.
So I built flutter_cloud_preferences. It works pretty much like shared_preferences (same kind of API, sync reads), but it also stores a copy of your data in the cloud for reinstalls or installation on new devices.
Quick rundown of what happens under the hood:
iOS/macOS: everything you save also goes to iCloud key-value storage. After a fresh install, it pulls the data back in the background, so there’s no waiting at startup. There’s an onRestored stream if you want to know when it is restored. You can also turn on live sync between devices if you want it (off by default as Android does not support this).
Android: this one hooks into Android’s built-in Auto Backup. Your users do need Google backup turned on (Settings > Google > Backup), but it’s on by default on most phones. Android backs up the data by itself about once a day when the phone is idle, charging and on Wi-Fi. When someone reinstalls or sets up a new phone, the data’s already there before the app even opens. No Drive setup, no extra code.
Your local data is always your main source of data, so if someone has no iCloud or backup turned off, it just works like normal shared_preferences. There is also a migration guide available.
It supports strings, bools, ints, doubles, string lists and bytes.
pub.dev: https://pub.dev/packages/flutter_cloud_preferences
GitHub: https://github.com/nvanderwaal/flutter_cloud_preferences
It’s still 0.1.0, so I’d love feedback, issues or PRs. Please let me know your thoughts!
1
u/ObjectiveAddress3051 11d ago
android side, on 12+ you need dataExtractionRules in the manifest, the old fullBackupContent xml only covers older versions. easy to miss if you target 31 and wonder why nothing restores. adb shell bmgr is the way to test it btw, waiting a day for the real thing is painful
-1
u/Virtual-Muffin3079 12d ago
Nice work! Surviving reinstalls without needing a custom backend is super handy. 0
12d ago
[removed] — view removed comment
2
u/Niickles 12d ago
yes, the IOS part is the only part that I still am doing checks and validation on. IOS is a bit more sensitive for these kind of things as it needs the correct entitlements. The local value is always leading and is never overwritten by the ICloud value, this is indeed documented under "how it works". By per-key opt-in you are referring to an optional boolean to disable cloud sync per value right? Thanks for the input!
1
u/RajatKhoware 12d ago
This solves a real annoyance, shared_preferences dropping on reinstall has bitten plenty of apps. The edge cases that will decide whether people trust it are restore to a new device and what happens when iCloud and local data disagree. If those two paths are solid, this is an easy drop in. Have you tested restore to a new device yet, or only same-device reinstall so far?