首页 > 解决方案 > Dart Flutter - 使用 Chopper 获取 WordPress 自定义帖子类型

问题描述

我正在使用 Google Flutter Framework 构建移动设备,并使用 Wordpress 作为我的应用程序的后端。我想使用 Flutter API 的 Chopper Retrofit 在 Flutter 中以 JSON 格式获取 Wordpress 自定义帖子类型数据。

谁能帮我构建一个示例代码,以便我可以轻松开始?我使用过这个(Flutter Wordpress),但我不知道如何使用自定义帖子类型。

https://github.com/dreamsoftin/flutter_wordpress

或者,如果有人知道如何使用它并获取自定义帖子类型,那么对我来说会更容易。

请帮忙!谢谢!

标签: wordpressflutterdartchopper

解决方案


查看您要用于构建移动应用程序的包,没有自定义类型的示例和解决方案(https://github.com/dreamsoftin/flutter_wordpress),但您可以分叉它,并将其扩展为特定的自定义帖子类型。我将向您展示如何执行此操作的示例(不包括自定义字段):

flutter_wordpress/lib/constants.dart

在第 10 行之后添加 const URL_POSTS = '$URL_WP_BASE/posts';

自定义帖子的端点行。假设您有自定义帖子,您将添加一个端点books

const URL_BOOKS = '$URL_WP_BASE/books';

在此处查看有关此内容以及如何为自定义帖子类型启用 REST API 的说明:

https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-rest-api-support-for-custom-content-types/#registering-a-custom-post-type-with -rest-api-支持

然后在flutter_wordpress/lib/requests/文件夹中,找到、克隆并重命名文件:

params_post_list.dartparams_book_list.dart

并将此处重命名class ParamsPostList为类ParamsBookList

在文件夹flutter_wordpress/lib/schemas/中找到

post.dart复制并重命名为book.dart

并将此处重命名class Post为类Book

然后在文件flutter_wordpress/lib/flutter_wordpress.dart 中

找到行import 'schemas/post.dart';,然后添加行import 'schemas/book.dart';

找到行 export 'requests/params_post_list.dart';,然后添加行export 'requests/params_book_list.dart';

找到行export 'schemas/post.dart';,然后添加行export 'schemas/book.dart';

然后找函数

async.Future<List<Post>> fetchPosts()

Future<Post> _postBuilder()

async.Future<Post> createPost({@required Post post})

复制这些函数重命名并替换PostBook(区分大小写)

注意:URL_POSTS在复制的函数中查找并重命名为URL_BOOKS


推荐阅读