Flutter Android端Amplify DataStore无法同步问题求助
问题解决:Flutter Amplify DataStore Android端clear后无法重新同步云端数据
问题现象
- iOS端运行正常,Android端构建运行时DataStore无法与云端同步,sync expression失效导致应用无法使用
- 已尝试调试/非调试模式,Amplify和Flutter均为最新版本
- 已通过DataStore订阅检查就绪状态后执行查询,无效
- 更换多个Android模拟器测试依旧失败,iOS模拟器正常
- Android端DataStore未从云端下载任何数据,
datastore hublisten无HubEvents返回 - 核心触发点:应用启动时DataStore同步正常,但执行
Amplify.Datastore.clear()和Amplify.Datastore.start()后无法同步
执行clear和start时的关键日志(已翻译为中文)
I/amplify:aws-datastore( 5384): 获取Orchestrator锁。 I/amplify:aws-datastore( 5384): DataStore插件初始化完成。 I/amplify:aws-datastore( 5384): Orchestrator 从SYNC_VIA_API状态切换到STOPPED状态 I/amplify:aws-datastore( 5384): 将当前状态设置为LOCAL_ONLY I/amplify:aws-datastore( 5384): 停止订阅处理器。 I/amplify:aws-datastore( 5384): 订阅处理器已停止。 I/amplify:aws-datastore( 5384): 停止监听本地存储变化。 I/amplify:aws-datastore( 5384): 将当前状态设置为STOPPED I/amplify:aws-datastore( 5384): 释放Orchestrator锁。 I/amplify:aws-datastore( 5384): 创建表: LastSyncMetadata I/amplify:aws-datastore( 5384): 创建表: PersistentModelVersion I/amplify:aws-datastore( 5384): 创建表: Matches I/amplify:aws-datastore( 5384): 创建表: User I/amplify:aws-datastore( 5384): 创建表: PersistentRecord I/amplify:aws-datastore( 5384): 创建表: ModelMetadata I/amplify:aws-datastore( 5384): 为表PersistentRecord创建索引 I/amplify:flutter:datastore( 5384): 成功清除存储 I/amplify:aws-datastore( 5384): 获取Orchestrator锁。 I/amplify:aws-datastore( 5384): DataStore插件初始化完成。 I/amplify:aws-datastore( 5384): Orchestrator 从STOPPED状态切换到SYNC_VIA_API状态 I/amplify:aws-datastore( 5384): 开始监听本地存储变化。 I/amplify:aws-datastore( 5384): 已开始监听本地存储。本地变化将被加入mutation outbox队列。 I/amplify:aws-datastore( 5384): 将当前状态设置为LOCAL_ONLY I/amplify:aws-datastore( 5384): 将当前状态设置为SYNC_VIA_API I/amplify:aws-datastore( 5384): 释放Orchestrator锁。 I/amplify:flutter:datastore( 5384): 成功启动datastore远程同步 I/amplify:aws-datastore( 5384): 启动API同步模式。 I/amplify:aws-datastore( 5384): 获取Orchestrator锁。 I/amplify:aws-datastore( 5384): DataStore插件初始化完成。 I/amplify:aws-datastore( 5384): 释放Orchestrator锁。 I/amplify:aws-datastore( 5384): 开始处理订阅事件。 I/amplify:aws-api( 5384): 无活跃订阅,关闭WebSocket连接。
排查与解决方案
从日志可见,执行start后WebSocket直接关闭,说明订阅未正常建立。针对Android端这个问题,可尝试以下方案:
强制重置同步元数据
调用clear()后,手动删除本地同步元数据文件(Android路径:/data/data/[你的包名]/files/amplify_datastore),再调用start()。或者通过代码重置LastSyncMetadata:await Amplify.DataStore.clear(); // 额外重置同步元数据 final syncMetadata = await Amplify.DataStore.query(LastSyncMetadata.classType); for (var meta in syncMetadata) { await Amplify.DataStore.delete(meta); } await Amplify.DataStore.start();调整start的执行时机
不要在clear完成后立即调用start,等待1-2秒确保本地存储完全清理完毕:await Amplify.DataStore.clear(); await Future.delayed(const Duration(seconds: 2)); await Amplify.DataStore.start();检查Android网络权限
确认AndroidManifest.xml中已添加必要的网络权限:<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />禁用WebSocket复用
在Amplify API配置中关闭WebSocket复用:Amplify.addPlugin(AmplifyAPI( modelProvider: ModelProvider.instance, apiAuthProviders: [AWSIamAuthProvider()], config: APIConfig( endpoints: [ EndpointConfig( name: "your-api-name", endpoint: "https://your-api-endpoint", websocketConfig: WebsocketConfig( enableReuse: false, ), ), ], ), ));降级Amplify版本
若最新版本存在兼容性问题,可尝试降级到Amplify Flutter 1.12.0版本,该版本在Android端clear/start同步逻辑更稳定。
内容的提问来源于stack exchange,提问作者Ishan Patel
相关产品推荐
相关产品推荐

