androidflutteraws-amplifyaws-datastore

Amplify datastore not syncing in flutter for Android


I have a flutter app that runs as intended on iOS. However, when I build it and run on Android, datastore doesn't sync with cloud and sync expression don't work which makes the app unusable. I tried running it in debug and non-debug mode. Amplify and flutter are up to date as well. I even used the datastore subscription to check for ready status then did queries but that did not work.

I have tried using different Android simulators, but it does not work. It works when I run on iOS simulators. After more debugging, Datastore doesn't seem to download anything from cloud and the datastore hublisten function doesn't return any hubevents either on Android.

Here are the logs. When I call Amplify.Datastore.clear() and Amplify.Datastore.start() these are the logs that show. Datastore syncs fine when the app launches, but when I clear() and start() it doesn't sync.

I/amplify:aws-datastore( 5384): Orchestrator lock acquired.
I/amplify:aws-datastore( 5384): DataStore plugin initialized.
I/amplify:aws-datastore( 5384): Orchestrator transitioning from SYNC_VIA_API to STOPPED
I/amplify:aws-datastore( 5384): Setting currentState to LOCAL_ONLY
I/amplify:aws-datastore( 5384): Stopping subscription processor.
I/amplify:aws-datastore( 5384): Stopped subscription processor.
I/amplify:aws-datastore( 5384): Stopping observation of local storage changes.
I/amplify:aws-datastore( 5384): Setting currentState to STOPPED
I/amplify:aws-datastore( 5384): Orchestrator lock released.
I/amplify:aws-datastore( 5384): Creating table: LastSyncMetadata
I/amplify:aws-datastore( 5384): Creating table: PersistentModelVersion
I/amplify:aws-datastore( 5384): Creating table: Matches
I/amplify:aws-datastore( 5384): Creating table: User
I/amplify:aws-datastore( 5384): Creating table: PersistentRecord
I/amplify:aws-datastore( 5384): Creating table: ModelMetadata
I/amplify:aws-datastore( 5384): Creating index for table: PersistentRecord
I/amplify:flutter:datastore( 5384): Successfully cleared the store
I/amplify:aws-datastore( 5384): Orchestrator lock acquired.
I/amplify:aws-datastore( 5384): DataStore plugin initialized.
I/amplify:aws-datastore( 5384): Orchestrator transitioning from STOPPED to SYNC_VIA_API
I/amplify:aws-datastore( 5384): Starting to observe local storage changes.
I/amplify:aws-datastore( 5384): Now observing local storage. Local changes will be enqueued to mutation outbox.
I/amplify:aws-datastore( 5384): Setting currentState to LOCAL_ONLY
I/amplify:aws-datastore( 5384): Setting currentState to SYNC_VIA_API
I/amplify:aws-datastore( 5384): Orchestrator lock released.
I/amplify:flutter:datastore( 5384): Successfully started datastore remote synchronization
I/amplify:aws-datastore( 5384): Starting API synchronization mode.
I/amplify:aws-datastore( 5384): Orchestrator lock acquired.
I/amplify:aws-datastore( 5384): DataStore plugin initialized.
I/amplify:aws-datastore( 5384): Orchestrator lock released.
I/amplify:aws-datastore( 5384): Starting processing subscription events.
I/amplify:aws-api( 5384): No more active subscriptions. Closing web socket.

enter code here

Solution

  • The actual error was datastore ready event not firing because the Amplify.Datastore.clear() method was interfering with Amplify.Datastore.start() method. The fix is to add a delay after clear() method like this:

    await Amplify.DataStore.clear(); 
    await Future.delayed(const Duration(seconds: 1)); 
    await Amplify.DataStore.start();