Page updated Jan 16, 2024

Read 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.

Query item

Now that you were able to make a mutation, take the id from the created Todo instance and use it to retrieve data.

1Future<Todo?> queryItem(Todo queriedTodo) async {
2 try {
3 final request = ModelQueries.get(Todo.classType, queriedTodo.id);
4 final response = await Amplify.API.query(request: request).response;
5 final todo = response.data;
6 if (todo == null) {
7 print('errors: ${response.errors}');
8 }
9 return todo;
10 } on ApiException catch (e) {
11 print('Query failed: $e');
12 return null;
13 }
14}

List items

You can get the list of items in Amplify.API.query:

1Future<List<Todo?>> queryListItems() async {
2 try {
3 final request = ModelQueries.list(Todo.classType);
4 final response = await Amplify.API.query(request: request).response;
5
6 final todos = response.data?.items;
7 if (todos == null) {
8 print('errors: ${response.errors}');
9 return <Todo?>[];
10 }
11 return todos;
12 } on ApiException catch (e) {
13 print('Query failed: $e');
14 }
15 return <Todo?>[];
16}

List subsequent pages of items

For large data sets, you'll need to paginate through the results. After receiving the first page of results, you can check if there is a subsequent page and obtain the next page.

1const limit = 100;
2
3Future<List<Todo?>> queryPaginatedListItems() async {
4 final firstRequest = ModelQueries.list<Todo>(Todo.classType, limit: limit);
5 final firstResult = await Amplify.API.query(request: firstRequest).response;
6 final firstPageData = firstResult.data;
7
8 // Indicates there are > 100 todos and you can get the request for the next set.
9 if (firstPageData?.hasNextResult ?? false) {
10 final secondRequest = firstPageData!.requestForNextResult;
11 final secondResult = await Amplify.API.query(request: secondRequest!).response;
12 return secondResult.data?.items ?? <Todo?>[];
13 } else {
14 return firstPageData?.items ?? <Todo?>[];
15 }
16}

Query Predicates

Models also support the use of query predicates for comparison. These are accessible from the Model's attributes, for example Blog["attribute"]["operator"].

Supported operators:

  • eq - equal
  • ne - not equal
  • gt - greater than
  • ge - greater than or equal
  • lt - less than
  • le - less than or equal
  • between - Matches models where the given field begins with the provided value.
  • beginsWith - Matches models where the given field is between the provided start and end values.
  • contains - Matches models where the given field contains the provided value.

Basic Equality Operator

Query for equality on a model's attribute.

1const blogTitle = 'Test Blog 1';
2final queryPredicate = Blog.NAME.eq(blogTitle);
3
4final request = ModelQueries.list<Blog>(Blog.classType, where: queryPredicate);
5final response = await Amplify.API.query(request: request).response;
6final blogFromResponse = response.data?.items.first;

Fetch by Parent ID

Get all Posts by parent ID

1final blogId = blog.id;
2
3final request =
4 ModelQueries.list(Post.classType, where: Post.BLOG.eq(blogId));
5final response = await Amplify.API.query(request: request).response;
6final data = response.data?.items ?? <Post?>[];

Less than

Return Posts with a rating less than 5.

1const rating = 5;
2
3final request = ModelQueries.list(Post.classType, where: Post.RATING.lt(rating));
4final response = await Amplify.API.query(request: request).response;
5
6final data = response.data?.items ?? <Post?>[];