Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wpResponse.data is empty #32

Closed
menuology-chris opened this issue Aug 18, 2023 · 5 comments
Closed

wpResponse.data is empty #32

menuology-chris opened this issue Aug 18, 2023 · 5 comments
Assignees

Comments

@menuology-chris
Copy link

menuology-chris commented Aug 18, 2023

Hi-
Trying to implement this on an app I'm building but even with a WordpressSuccessResponse() the data varaible is empty in wpResponse.data

`
void fetchArticles() async {
final baseUrl = Uri.parse('https://mydomain.com/wp-json/wp/v2');
final client = WordpressClient(
baseUrl: baseUrl,
bootstrapper: (bootstrapper) => bootstrapper
.withStatisticDelegate((baseUrl, requestCount) {
//print('$baseUrl -> $requestCount');
})
.withDebugMode(true)
.build(),
);
client.initialize();

final request = ListPostRequest(
  page: 1,
  perPage: 1,
  order: Order.asc,
);

final wpResponse = await client.posts.list(request);
switch (wpResponse) {
  case WordpressSuccessResponse():
    final data = wpResponse.data; // List<Post>
    break;
  case WordpressFailureResponse():
    final error = wpResponse.error; // WordpressError
    break;
}

setState(() {});

}
`

With debug enabled, I can see the successful connection and even the WP data in the Response Text field of the response, but the wpResponse.data field remains empty.

Logs:
scratch_1.txt

Is this a bug or am I just implementing it incorrectly? Thanks for your help!

@menuology-chris
Copy link
Author

Oh, this is on 8.0.7

wordpress_client: ^8.0.7

@ArunPrakashG ArunPrakashG self-assigned this Aug 19, 2023
@ArunPrakashG
Copy link
Owner

Hey @menuology-chris
I attempted to replicate the scenario you described and obtained the expected results. I used the same domain as the one in the log file you provided.

I suspect that the issue stems from your codebase. Since you are calling setState immediately after the network call, could you please verify your program flow? That might be causing the problem.

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

  // Simple Usage
  final client = WordpressClient(
    baseUrl: baseUrl,
  );

  client.initialize();

  final response = await client.posts.list(
    ListPostRequest(
      perPage: 1,
      order: Order.asc,
    ),
  );

  response.map<void>(
    onSuccess: (response) {
      print('Posts Count: ${response.data.length}');
      // prints Posts Count: 1, as expected
    },
    onFailure: (response) {
      print(response.error.toString());
    },
  );

@menuology-chris
Copy link
Author

menuology-chris commented Aug 20, 2023

Thanks for the quick reply.

So interestingly, all I did was switch out the response code from the Dart style to the WP style as you referenced

final wpResponse = await client.posts.list(request);
switch (wpResponse) {
  case WordpressSuccessResponse():
    final data = wpResponse.data; // List<Post>
    break;
  case WordpressFailureResponse():
    final error = wpResponse.error; // WordpressError
    break;
}

to

response.map<void>(
    onSuccess: (response) {
      print('Posts Count: ${response.data.length}');
    },
    onFailure: (response) {
      print(response.error.toString());
    },
  );

And now it works

void fetchArticles() async {
    final baseUrl = Uri.parse('https://{mydomain}.com/wp-json/wp/v2');

    final client = WordpressClient(
      baseUrl: baseUrl,
    );

    client.initialize();

    final response = await client.posts.list(
      ListPostRequest(
        perPage: 20,
        order: Order.asc,
      ),
    );

    response.map<void>(
      onSuccess: (response) {
        print(response.data);
      },
      onFailure: (response) {
        print(response.error.toString());
      },
    );

    setState(() {});
  }

Could there be something different with the Dart 3 style that would cause it to be missing the data?

@ArunPrakashG
Copy link
Owner

@menuology-chris I had tried both methods before sharing the previous update and it was working for me. I have also retried with the same sample and it was still working on both approaches. There are no differences between the two approach. Could you please check again?

@ArunPrakashG
Copy link
Owner

Closing this due to inactivity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants