Remove DataStore
Before migrating your DataStore calls, remove Amplify DataStore from your project and configure the API plugin to use your models.
Update dependencies
Remove the amplify_datastore package from your pubspec.yaml and ensure the amplify_api package is present:
dependencies: # Remove this line: # amplify_datastore: ^2.x.x
# Keep or add these: amplify_flutter: ^2.x.x amplify_api: ^2.x.x amplify_auth_cognito: ^2.x.x # if using authAfter updating pubspec.yaml, run:
flutter pub getUpdate Amplify configuration
When using DataStore, the ModelProvider was passed to the AmplifyDataStore plugin. After removing DataStore, you must pass ModelProvider.instance to the AmplifyAPI plugin instead. Without this, ModelMutations and ModelQueries cannot serialize and deserialize your models, and API calls will fail at runtime.
Before (with DataStore):
import 'package:amplify_datastore/amplify_datastore.dart';import 'package:amplify_api/amplify_api.dart';import 'models/ModelProvider.dart';
final datastorePlugin = AmplifyDataStore(modelProvider: ModelProvider.instance);final api = AmplifyAPI();
await Amplify.addPlugins([datastorePlugin, api]);await Amplify.configure(amplifyConfig);After (API only):
import 'package:amplify_api/amplify_api.dart';import 'package:amplify_api/model_queries.dart';import 'package:amplify_api/model_mutations.dart';import 'package:amplify_api/model_subscriptions.dart';import 'models/ModelProvider.dart';
// Pass ModelProvider to the API pluginfinal api = AmplifyAPI( options: APIPluginOptions(modelProvider: ModelProvider.instance),);
await Amplify.addPlugins([api]);await Amplify.configure(amplifyConfig);Remove all DataStore calls
Search your codebase and remove or replace all DataStore calls:
| DataStore Call | API Replacement |
|---|---|
Amplify.DataStore.save() | Amplify.API.mutate() with ModelMutations.create() / .update() |
Amplify.DataStore.delete() | Amplify.API.mutate() with ModelMutations.delete() |
Amplify.DataStore.query() | Amplify.API.query() with ModelQueries.get() / .list() |
Amplify.DataStore.observe() | Amplify.API.subscribe() with ModelSubscriptions.onCreate/onUpdate/onDelete() |
Amplify.DataStore.observeQuery() | Initial list query + three subscriptions (see Migrate to Amplify API) |
Amplify.DataStore.clear() | No longer needed (no local DataStore to clear) |
Amplify.DataStore.start() / stop() | No longer needed |