-
Notifications
You must be signed in to change notification settings - Fork 14
Sending Requests
Arun Prakash edited this page Sep 21, 2025
·
4 revisions
Interact with the WordPress REST API by sending requests. For example, to fetch the latest 20 posts in ascending order:
final request = ListPostRequest(
page: 1,
perPage: 20,
order: Order.asc,
);
final wpResponse = await client.posts.list(request);
// Handle response
final result = wpResponse.map(
onSuccess: (response) {
print(response.message);
return response.data;
},
onFailure: (response) {
print(response.error.toString());
return <Post>[];
},
);
// Dart 3 pattern matching
switch (wpResponse) {
case WordpressSuccessResponse():
final posts = wpResponse.data; // typed List<Post>
break;
case WordpressFailureResponse():
final err = wpResponse.error;
break;
}
// Expect success or throw a helpful error
final posts = (await client.posts.list(ListPostRequest(perPage: 10))).dataOrThrow();
// Convenience extension helpers also exist
final one = await client.posts.extensions.getById(123);
Utilize the WordpressResponse
class to access metadata related to the response, such as request duration and status codes. Differentiate between success and failure responses to handle data appropriately.