top of page

Guide to integrate and use AWS Amplify and AWS AppSync with Flutter mobile apps


Guide to integrate and use AWS Amplify with Flutter mobile apps

Flutter is a cross-platform mobile development framework that allows you to build native apps for iOS and Android from a single codebase. AWS Amplify is a set of tools and services that make it easy to build and deploy cloud-powered mobile apps. It also supports local persistence with automatic sync with cloud data store.


In this blog post, we will show you how to build a CRUD Flutter mobile app using AWS Amplify and AWS AppSync. We will create a simple app that allows users to create, read, update, and delete trips.


Prerequisites


To follow this blog post, you will need the following:

  • A Flutter development environment

  • An AWS account

  • The AWS Amplify CLI


Step 1: Create a new Flutter project


First, we need to create a new Flutter project. We can do this by running the following command in the terminal:

flutter create amplify_crud_app 

This will create a new Flutter project called amplify_crud_app.


Step 2: Initialize AWS Amplify


Next, we need to initialize AWS Amplify in our Flutter project. We can do this by running the following command in the terminal:

amplify init 

The amplify init command will initialize AWS Amplify in your Flutter project. This command will create a new file called amplifyconfiguration.json in the root directory of your project. This file will contain the configuration settings for your AWS Amplify project.

When you run the amplify init command, you will be prompted to answer a few questions about your project. These questions include:

  • The name of your project

  • The region that you want to deploy your project to

  • The environment that you want to create (e.g., dev, staging, prod)

  • The type of backend that you want to use (e.g., AWS AppSync, AWS Lambda)

Once you have answered these questions, the amplify init command will create the necessary resources in AWS.


Step 3: Configure AWS Amplify


Once you have initialized AWS Amplify, you need to configure it. You can do this by running the following command in the terminal:

amplify configure 

This command will open a wizard that will guide you through the process of configuring AWS Amplify.


When you run the amplify configure command, you will be prompted to enter your AWS credentials. You can also choose to configure other settings, such as the name of your app, the region that you want to deploy your app to, and the environment that you want to use.


Step 4: Creating a GraphQL API


The amplify add api command will create a GraphQL API in AWS AppSync. This GraphQL API will allow us to interact with the data in our Trip data model.


The amplify add api command will prompt you to enter a few details about the GraphQL API that you want to create. These details include:

  • The name of the GraphQL API

  • The schema for the GraphQL API

  • The authentication method for the GraphQL API

Once you have entered these details, the amplify add api command will create the GraphQL API in AWS AppSync.


The Trip schema


The Trip schema will define the structure of the data that we can query and mutate in our GraphQL API. The Trip schema will include the following fields:

  • id: The ID of the trip. This field will be a unique identifier for the trip.

  • name: The name of the trip.

  • destination: The destination of the trip.

  • startDateTime: The start date and time of the trip.

  • endDateTime: The end date and time of the trip.

These are just a few examples of the fields that you could include in your Trip schema. You can customize the schema to meet the specific needs of your application.


Authentication


The amplify add api command will also prompt you to choose an authentication method for your GraphQL API. You can choose to use Amazon Cognito or AWS IAM for authentication.


If you choose to use Amazon Cognito, you will need to create a user pool and a user pool client. You can do this by using the AWS Management Console or the AWS CLI.


Once you have created a user pool and a user pool client, you can configure your GraphQL API to use Amazon Cognito for authentication.


Step 5: Creating a data model


We need to create a data model for our CRUD Flutter mobile app. This data model will define the structure of the data that we will store in AWS AppSync.


To create a data model, we need to run the following command in the terminal:

amplify add api --model Trip 

This will create a data model called Trip.


The amplify add api --model Trip command will create a data model called Trip in AWS AppSync. This data model will define the structure of the data that we will store in AWS AppSync.


The amplify add api --model command will prompt you to enter a few details about the data model that you want to create. These details include:

  • The name of the data model

  • The fields that you want to include in the data model

  • The types of the fields

Once you have entered these details, the amplify add api --model command will create the data model in AWS AppSync.


The Trip data model


The Trip data model that we will create in this blog post will have the following fields:

  • id: The ID of the trip. This field will be a unique identifier for the trip.

  • name: The name of the trip.

  • destination: The destination of the trip.

  • startDateTime: The start date and time of the trip.

  • endDateTime: The end date and time of the trip.

These are just a few examples of the fields that you could include in your Trip data model. You can customize the fields in your data model to meet the specific needs of your application.


Step 6: Implementing the CRUD operations


Once we have created the data model and the GraphQL API, we need to implement the CRUD operations for our CRUD Flutter mobile app. This means that we need to implement code to create, read, update, and delete trips.


We can implement the CRUD operations by using the amplify-flutter library. This library provides us with a set of widgets that we can use to interact with AWS AppSync. The data will be persisted locally first, and if the internet connectivity is available it will sync with cloud.


The amplify-flutter library includes a widget called AmplifyDataStore. This widget allows us to interact with the data in our Trip data model.


Here is an example:


To create a trip, we can use the Amplify.DataStore.save() method provided by amplify_flutter. Let's take a look at the code snippet below:

final trip = Trip(
  name: 'My Trip',
  destination: 'London',
  startDateTime: DateTime.now(),
  endDateTime: DateTime.now().add(Duration(days: 7)),
);

try {
  await Amplify.DataStore.save(trip);
  print('Trip created successfully');
} catch (e) {
  print('Error creating trip: $e');
}

To read a specific trip from the data store, we can utilize the Amplify.DataStore.query() method. Let's see how it's done:

final tripId = '1234567890';

try {
  final trip = await Amplify.DataStore.query(Trip.classType, where: {
    'id': tripId,
  });

  print('Trip: ${trip.name}');
} catch (e) {
  print('Error reading trip: $e');
}

To update a trip, we need to retrieve it from the data store, modify its properties, and save it back using the Amplify.DataStore.save() method. Here's an example:

final tripId = '1234567890';
final newName = 'My New Trip';

try {
  final trip = await Amplify.DataStore.query(Trip.classType, where: {
    'id': tripId,
  });

  trip.name = newName;

  await Amplify.DataStore.save(trip);
  print('Trip updated successfully');
} catch (e) {
  print('Error updating trip: $e');
}

To delete a trip from the data store, we can use the Amplify.DataStore.delete() method. Here's an example:

final tripId = '1234567890';

try {
  await Amplify.DataStore.delete(Trip.classType, where: {
    'id': tripId,
  });
  print('Trip deleted successfully');
} catch (e) {
  print('Error deleting trip: $e');
}

Step 6: Run the app


Once we have implemented the CRUD operations, we can run the app. To do this, we can run the following command in the terminal:

flutter run 

This will run the app in the emulator or on a physical device.


Conclusion


In this blog post, we showed you how to build a CRUD Flutter mobile app using AWS Amplify. We created a simple app that allows users to create, read, update, and delete trips.


I hope you found this blog post helpful. If you have any questions, please leave a comment below.



Blog for Mobile App Developers, Testers and App Owners

 

This blog is from Finotes Team. Finotes is a lightweight mobile APM and bug detection tool for iOS and Android apps.

​

In this blog we talk about iOS and Android app development technologies, languages and frameworks like Java, Kotlin, Swift, Objective-C, Dart and Flutter that are used to build mobile apps. Read articles from Finotes team about good programming and software engineering practices, testing and QA practices, performance issues and bugs, concepts and techniques. 

bottom of page