You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter物理设备卸载重装后get_storage数据仍保留求助

解决get_storage卸载重装后数据残留问题

核心原因

get_storage默认将数据存储在应用的持久化目录(Android的getApplicationDocumentsDirectory、iOS的NSDocumentDirectory),而部分平台的自动备份机制(Android Auto Backup、iOS iCloud Backup)会在卸载后保留这些数据,重装时自动恢复。


解决方案

1. 关闭Android自动备份(针对Android平台)

直接禁止应用整体备份,或仅排除get_storage的数据:

  • 完全禁止备份:打开android/app/src/main/AndroidManifest.xml,在<application>标签中添加:
    android:allowBackup="false"
    
  • 仅排除get_storage数据:
    1. 在android/app/src/main/res/xml下创建backup_rules.xml,内容如下:
      <?xml version="1.0" encoding="utf-8"?>
      <full-backup-content>
          <exclude domain="sharedpref" path="get_storage" />
      </full-backup-content>
      
    2. 在AndroidManifest.xml的<application>标签中添加:
      android:fullBackupContent="@xml/backup_rules"
      

2. 关闭iOS iCloud备份(针对iOS平台)

修改ios/Runner/Info.plist,添加配置禁止iCloud备份应用数据:

<key>NSUbiquitousContainerIsDocumentScopePublic</key>
<false/>
<key>NSUbiquitousContainerName</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>NSUbiquitousContainerSupportedFolderLevels</key>
<string>None</string>

3. 代码层面主动清理数据

在应用启动时判断是否为重装后的首次运行,主动清除get_storage数据:

  • 示例1:通过应用版本号判断(适用于版本更新+重装场景)
    import 'package:get_storage/get_storage.dart';
    import 'package:package_info_plus/package_info_plus.dart';
    
    void main() async {
      await GetStorage.init();
      final storage = GetStorage();
      final packageInfo = await PackageInfo.fromPackageInfo();
      final savedVersion = storage.read('app_version');
    
      if (savedVersion != packageInfo.version) {
        await storage.clear();
        await storage.write('app_version', packageInfo.version);
      }
    
      runApp(MyApp());
    }
    
  • 示例2:通过首次安装时间判断(精准识别重装)
    import 'package:get_storage/get_storage.dart';
    import 'package:device_info_plus/device_info_plus.dart';
    import 'dart:io';
    
    void main() async {
      await GetStorage.init();
      final storage = GetStorage();
      final deviceInfo = DeviceInfoPlugin();
      DateTime firstInstallTime;
    
      if (Platform.isAndroid) {
        final androidInfo = await deviceInfo.androidInfo;
        firstInstallTime = DateTime.fromMillisecondsSinceEpoch(androidInfo.firstInstallTime);
      } else if (Platform.isIOS) {
        final iosInfo = await deviceInfo.iosInfo;
        firstInstallTime = iosInfo.systemInstallTime;
      } else {
        firstInstallTime = DateTime.now();
      }
    
      final savedInstallTime = storage.read('first_install_time');
      if (savedInstallTime == null || DateTime.parse(savedInstallTime) != firstInstallTime) {
        await storage.clear();
        await storage.write('first_install_time', firstInstallTime.toIso8601String());
      }
    
      runApp(MyApp());
    }
    

4. 修改get_storage存储位置到缓存目录

将数据存储到系统缓存目录,卸载应用时系统会自动清理缓存:

import 'package:get_storage/get_storage.dart';
import 'package:path_provider/path_provider.dart';

void main() async {
  final cacheDir = await getTemporaryDirectory();
  await GetStorage.init(
    storageDirectory: cacheDir.path,
  );
  runApp(MyApp());
}

注意:缓存目录的数据可能被系统在低内存时自动清理,不适合存储重要持久化数据。


内容的提问来源于stack exchange,提问作者deok

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 13:45:32