Read application data
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(4 Todo.classType,5 queriedTodo.modelIdentifier,6 );7 final response = await Amplify.API.query(request: request).response;8 final todo = response.data;9 if (todo == null) {10 safePrint('errors: ${response.errors}');11 }12 return todo;13 } on ApiException catch (e) {14 safePrint('Query failed: $e');15 return null;16 }17}
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 safePrint('errors: ${response.errors}');9 return const [];10 }11 return todos;12 } on ApiException catch (e) {13 safePrint('Query failed: $e');14 return const [];15 }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 =12 await Amplify.API.query(request: secondRequest!).response;13 return secondResult.data?.items ?? <Todo?>[];14 } else {15 return firstPageData?.items ?? <Todo?>[];16 }17}
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
- equalne
- not equalgt
- greater thange
- greater than or equallt
- less thanle
- less than or equalbeginsWith
- Matches models where the given field begins with the provided value.between
- 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>(5 Blog.classType,6 where: queryPredicate,7);8final response = await Amplify.API.query(request: request).response;9final blogFromResponse = response.data?.items.first;
Fetch by Parent ID
Get all Posts by parent ID
1final blogId = blog.id;2
3final request = ModelQueries.list(4 Post.classType,5 where: Post.BLOG.eq(blogId),6);7final response = await Amplify.API.query(request: request).response;8final data = response.data?.items ?? <Post?>[];
Less than
Return Posts with a rating less than 5.
1const rating = 5;2
3final request = ModelQueries.list(4 Post.classType,5 where: Post.RATING.lt(rating),6);7final response = await Amplify.API.query(request: request).response;8
9final data = response.data?.items ?? <Post?>[];