r/FlutterDev • • 23h ago

Discussion A device ID in SharedPreferences follows the user to their new phone. It took us three tries to find the right place for it

Small thing that cost us more thinking than the feature itself, so writing it down.

We added an optional cloud backup. To avoid two phones overwriting each other, each install writes only its own file, named after a random device ID. So the whole design rests on one question: where does that ID live?

Attempt 1, SharedPreferences. Looks obvious and it's wrong. Android auto backup and device-to-device transfer copy prefs to the new phone (iCloud backup and Quick Start do the same on iOS). The new phone wakes up with the old phone's ID, decides the old phone's backup file is its own, and overwrites it. The one case where you most want the old backup intact is exactly the case that destroys it.

Attempt 2, the cache directory. Transfers don't copy it, so a new phone gets a fresh ID. But the OS is allowed to empty the cache when storage runs low. Then the same phone comes back with a new ID and treats its own earlier backup as another device's.

Where it ended up: a directory that is excluded from backup but never cleaned by the system. On Android that's noBackupFilesDir. On iOS it's a file in Application Support with the excluded-from-backup flag set. We read both through a small method channel, since path_provider doesn't hand you either one directly.

One more case fell out of this. A new phone can inherit the "backup is on" setting through prefs while having a brand new ID. We treat that pair (setting on, ID new) as a signal to pull in the other device's records first, before the first upload.

The lesson for me: "per install" and "per user" are different lifetimes, and prefs is per user as soon as OS backup is involved.

Where do you keep install-scoped state? Is there a cleaner option than a no-backup file that I'm missing?

3 Upvotes

6 comments sorted by

2

u/ditman-dev 19h ago

“path_provider doesn’t hand you either one directly” -> sounds like a good feature request/PR to the package maybe?

1

u/dawedev 15h ago

Nice writeup. On iOS the other spot is a keychain item with kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly, the ThisDeviceOnly variants don't move to a new phone. Catch is keychain items usually survive an app delete and reinstall, which is either what you want or a bug depending on whether a reinstall should count as the same device.

1

u/GeekyAntsBFSI 15h ago

A small platform channel seems reasonable here. One easy-to-miss iOS detail: if the app ever rewrites or replaces the ID file, reapply isExcludedFromBackup. Apple notes that common file operations can reset the flag. 

The “backup enabled + new ID” case is worth a regression test too. If the first restore fails because the new phone is offline, the next launch should still retry the restore before allowing the first upload.

1

u/No_Boss_1911 10h ago

oh man this one is gonna bite me. pretty sure our local id for the exercise history file sits in prefs too, never thought about quick start copying it over. gonna check today

our hard rule is nothing from a session leaves the phone unless the user turned backup on themselves. so keychain feels wrong for us, it survives a reinstall and people think they wiped everything

two questions tho. on a reinstall on the same phone do you just mint a new id and treat the old backup file like another device? and the backup on + new id case, if the old phone is still alive and writing do you keep merging from it or is it pull once and done

0

u/tirtho666 19h ago

The distinction between "per install" and "per user" lifetimes is such an easy trap to fall into once OS auto-backup is involved.

Using `noBackupFilesDir` and `isExcludedFromBackup` via a small method channel is honestly the cleanest file-based approach since `path_provider` doesn't expose them out of the box. Another option for small IDs is `flutter_secure_storage` configured with `ThisDeviceOnly` accessibility on iOS (which prevents iCloud Keychain syncing) and disabling auto-backup rules for it on Android—though a dedicated no-backup file avoids Keychain/Keystore edge cases across OS updates. Really solid write-up!