Exception when getting posts with context as embed
This works fine without context being set to embed. The goal was to reduce the response size as I just needed Title, link, and Featured Image Tested in the Android 13 Emulator.
The Client
WordpressClient.fromDioInstance(baseUrl: Uri.parse('https://thetailcompany.com/wp-json/wp/v2'), instance: initDio());
The final request url which works in a browser
https://thetailcompany.com/wp-json/wp/v2/posts?context=embed&page=1&per_page=10&order=desc
Request code
final ListPostRequest request = ListPostRequest(
page: 1,
perPage: 10,
order: Order.desc,
context: RequestContext.embed,
);
final WordpressResponse<List<Post>> wordpressPostResponse = await client.posts.list(request);
Exception name: type 'Null' is not a subtype of type 'bool'
Stacktrace
#0 new Post.fromJson (package:wordpress_client/src/responses/post_response.dart:73:15)
#1 WordpressClient._registerInternalInterfaces.<anonymous closure> (package:wordpress_client/src/wordpress_client_base.dart:367:31)
#2 MappedListIterable.elementAt (dart:_internal/iterable.dart:425:31)
#3 ListIterator.moveNext (dart:_internal/iterable.dart:354:26)
#4 new _GrowableList._ofEfficientLengthIterable (dart:core-patch/growable_array.dart:189:27)
#5 new _GrowableList.of (dart:core-patch/growable_array.dart:150:28)
#6 new List.of (dart:core-patch/array_patch.dart:39:18)
#7 ListIterable.toList (dart:_internal/iterable.dart:224:7)
#8 IRequestExecutor.list.<anonymous closure> (package:wordpress_client/src/request_executor_base.dart:325:72)
#9 WordpressRawResponse.map (package:wordpress_client/src/responses/wordpress_raw_response.dart:207:21)
#10 IRequestExecutor.list (package:wordpress_client/src/request_executor_base.dart:322:24)
<asynchronous suspension>
#11 _TailBlogState.getFeed (package:tail_app/Frontend/Widgets/tail_blog.dart:111:67)
<asynchronous suspension>
the Dio client
Dio initDio({skipSentry = false}) {
final Dio dio = Dio();
dio.httpClientAdapter = NativeAdapter();
dio.interceptors.add(
LogInterceptor(
requestBody: false,
requestHeader: false,
responseBody: false,
responseHeader: false,
request: false,
logPrint: (o) => dioLogger.finer(o.toString()),
),
);
if (!skipSentry) {
dio.addSentry(failedRequestStatusCodes: []);
}
return dio;
}
Hi @Codel1417 ! I think the issue occurs because it expects a "sticky" boolean property in the response, which is non-nullable in the response model class. When using embed, some properties are not present in the response. This is currently not handled on the library, i will update the model classes accordingly and publish the updated version soon.
Meanwhile, you can use the listRaw request to get the unparsed, raw response from the API. you could then parse this directly into your own model class. This works with the Wordpress _field parameter which allows to specify what all fields to return in the response. This could be helpful for your usecase.
Additionally, if you are using embed just to get the featured image url, i had similar requirement and i wrote a simple, single file plugin for this purpose. You can download this file and place it in the plugins directory to get the functionality! https://github.com/ArunPrakashG/wordpress_client/blob/master/wordpress_client_helper/wordpress_client_helper.php
Thanks for the _field suggestion. It actually works with regular list response too
queryParameters: {'_fields': 'id,title,link,featured_media_src_url,featured_media,sticky,slug,author,date'},
I was able to significantly reduce the response size. I found that sticky, slug, and author were required even though I didn't need the values. Though they don't add much since I'm only requesting 10 posts in total