How to selectively sync down data from cloud to datastore in my flutter app

68 views Asked by At

I have two models in many to many relationship.

type Ingredient @model @auth(rules: [{allow: public}]) {
  id: ID!
  name: String
  Recipes: [Recipe] @manyToMany(relationName: "RecipeIngredient")
}

type Recipe @model @auth(rules: [{allow: public}]) {
  id: ID!
  name: String
  Ingredients: [Ingredient] @manyToMany(relationName: "RecipeIngredient")
}

How to sync down the Ingredients only if it has a Recipe with a particular recipe id?

I've read this page about selectively syncing a subset of the data. I know I have to write a datastore sync expression during configuration but I don't know how. It is what I've tried, but it doesn't work.

await Future.wait([
     Amplify.addPlugin(AmplifyDataStore(
         modelProvider: ModelProvider.instance,
         syncExpressions: [
             DataStoreSyncExpression(
                  Ingredient.classType,
                  () => Ingredient.RECIPES.contains(
                      RecipeIngredient.RECIPE.eq(RecipeModelIdentifier(id: recipeId))))
            ],
          )),
          Amplify.addPlugin(AmplifyAPI()),
          Amplify.addPlugin(AmplifyAuthCognito()),
          Amplify.addPlugin(AmplifyStorageS3()),
        ]);

await Amplify.configure(amplifyconfig);

Can anyone help me with this. Thank you so much.

1

There are 1 answers

0
oseh mathias On

1. Initialise with an empty expression:

 final datastorePlugin = AmplifyDataStore(
    modelProvider: ModelProvider.instance,
    syncExpressions: userId == null
        ? [] : [...expressions]

2. After login, set a global variable. Use this without the final constructor in your initialise section in step 1. Call stop, clear and start on login.