Skip to content
Arun Prakash edited this page Aug 7, 2023 · 12 revisions

You can use this library just like any other Dart package.

  • Add wordpress_client as a dependency on pubspec.yaml file on your project root. At the time of this writing, the latest package version is 6.3.3. Do check Package Page to get latest version.
dependencies:
  wordpress_client: ^8.0.0
  • Import the library to your project class in which you want to use the library.
import 'package:wordpress_client/wordpress_client.dart';
  • Initializing the client can be done in two ways. It is recommended to initialize WordpressClient once and assign the instance to a variable for later use. State of the properties are stored only in that particular instance of the client.

    • Simple method, Initialize with default values.
    • Advanced method (with Bootstrapper to configure various settings like User Agent, Authorization etc)

Simple method

final baseUrl = Uri.parse('https://example.com/wp-json/wp/v2');
final client = WordpressClient(baseUrl: baseUrl);

// Call this on splash screen / or anywhere where you have your initializing logic
client.initialize();

WordpressResponse is a class that encapsulates the actual result object (or error, depending on the result) and provides access to statistical data related to the response, such as the time taken for the request to complete, status codes, and the total number of pages.

Once WordpressResponse is received, It is important to compare the underlying type to WordpressSuccessResponse or WordpressFailureResponse to determine the actual result of the request. In order to do this, there are various getters and methods available on the type.

Once mapped, if the response is of type WordpressSuccessResponse, the resuling data of the response can be accessed on the data property. If the result is of type WordpressFailureResponse, the error details can be accessed on the error property.

Advanced method

final baseUrl = Uri.parse('https://example.com/wp-json/wp/v2');

final client = WordpressClient(
    baseUrl: baseUrl,
    bootstrapper: (bootstrapper) => bootstrapper
        .withStatisticDelegate((baseUrl, requestCount) {
          print('$baseUrl -> $requestCount');
        })
        .withDebugMode(true)
        .withDefaultAuthorization(
          UsefulJwtAuth(
            userName: 'username',
            password: 'password',
          ),
        )
        .build(),
);

client.initialize();
  • You are now prepared to send requests to the WordPress REST API. For instance, to retrieve the 20 most recent posts from your WordPress site in ascending order, you can use the following code snippet:
final request = ListPostRequest(
  page: 1,
  perPage: 20,
  order = Order.asc,
);

final wpResponse = await client.posts.list(request);

// Dart 3 style
switch (wpResponse) {
    case WordpressSuccessResponse():
      final data = wpResponse.data; // List<Post>
      break;
    case WordpressFailureResponse():
      final error = wpResponse.error; // WordpressError
      break;
}

// or
// wordpress_client style
final result = postsResponse.map(
    onSuccess: (response) {
      print(response.message);
      return response.data;
    },
    onFailure: (response) {
      print(response.error.toString());
      return <Post>[];
    },
);