Page updated Jan 16, 2024

Create, update, and delete application data

Amplify Flutter v0 is now in Maintenance Mode until July 19th, 2024. This means that we will continue to include updates to ensure compatibility with backend services and security. No new features will be introduced in v0.

Please use the latest version (v1) of Amplify Flutter to get started.

If you are currently using v0, follow these instructions to upgrade to v1.

Run a mutation

Now that the client is set up, you can run a GraphQL mutation with Amplify.API.mutate to create, update, and delete your data.

1Future<void> createTodo() async {
2 try {
3 final todo = Todo(name: 'my first todo', description: 'todo description');
4 final request = ModelMutations.create(todo);
5 final response = await Amplify.API.mutate(request: request).response;
6
7 final createdTodo = response.data;
8 if (createdTodo == null) {
9 safePrint('errors: ${response.errors}');
10 return;
11 }
12 safePrint('Mutation result: ${createdTodo.name}');
13 } on ApiException catch (e) {
14 safePrint('Mutation failed: $e');
15 }
16}

To update the Todo with a new name:

1Future<void> updateTodo(Todo originalTodo) async {
2 final todoWithNewName = originalTodo.copyWith(name: 'new name');
3
4 final request = ModelMutations.update(todoWithNewName);
5 final response = await Amplify.API.mutate(request: request).response;
6 print('Response: $response');
7}

To delete the Todo:

1Future<void> deleteTodo(Todo todoToDelete) async {
2 final request = ModelMutations.delete(todoToDelete);
3 final response = await Amplify.API.mutate(request: request).response;
4 print('Response: $response');
5}
1// or delete by ID, ideal if you do not have the instance in memory, yet
2Future<void> deleteTodoById(Todo todoToDelete) async {
3 final request = ModelMutations.deleteById(Todo.classType, '8e0dd2fc-2f4a-4dc4-b47f-2052eda10775');
4 final response = await Amplify.API.mutate(request: request).response;
5 print('Response: $response');
6}