# FormatException: Invalid radix-10 number (at character 1) while reading SharedPreferences

- **ID:** `flutter/shared-preferences-corruption`
- **Domain:** flutter
- **Category:** data_error
- **Verification:** ai_generated
- **Fix Rate:** 78%

## Root Cause

A value stored in SharedPreferences (or NSUserDefaults) was expected to be an integer but was corrupted or saved as a non-numeric string, often due to version mismatches or manual editing.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| Flutter 3.22 | active | — | — |
| Dart 3.4 | active | — | — |
| Android 13 | active | — | — |
| iOS 16 | active | — | — |

## Workarounds

1. **Use a migration function that reads the corrupted value as a string, attempts to parse it, and if it fails, removes the key and sets a default. Example: `final raw = prefs.getString('counter'); int? value; try { value = int.parse(raw!); } catch (e) { await prefs.remove('counter'); value = 0; }`** (85% success)
   ```
   Use a migration function that reads the corrupted value as a string, attempts to parse it, and if it fails, removes the key and sets a default. Example: `final raw = prefs.getString('counter'); int? value; try { value = int.parse(raw!); } catch (e) { await prefs.remove('counter'); value = 0; }`
   ```
2. **Implement a versioned SharedPreferences wrapper that validates data types on read and repairs corrupted entries automatically.** (75% success)
   ```
   Implement a versioned SharedPreferences wrapper that validates data types on read and repairs corrupted entries automatically.
   ```
3. **For critical data, migrate to a more robust storage solution like Hive or Isar that includes type safety and corruption detection.** (80% success)
   ```
   For critical data, migrate to a more robust storage solution like Hive or Isar that includes type safety and corruption detection.
   ```

## Dead Ends

- **** — This deletes all user preferences, causing loss of user settings and potentially breaking app functionality; it's a nuclear option that should only be used as a last resort. (40% fail)
- **** — The corruption remains in storage and will cause the same error on every app restart; the default value may not match user expectations. (70% fail)
- **** — Not feasible for production apps; root access is rare and editing XML can cause further corruption or crash the app. (95% fail)
