3

I am building a mobile with Google Flutter Framework and using Wordpress as the backend for my app. I want to fetch Wordpress Custom Post Type data in JSON format in Flutter with Chopper Retrofit of Flutter API.

Can anyone help me to build a sample code so I can get started with ease? I worked with this ( Flutter Wordpress ) but I don't know how to use Custom Post Types with this.

https://github.com/dreamsoftin/flutter_wordpress

Or if anyone knows how to use this and fetch custom post types then it would be more easy for me.

Please Help! Thank you!

Nikola Kirincic
  • 3,651
  • 1
  • 24
  • 28
Mayanktaker
  • 175
  • 5
  • 14

1 Answers1

1

Looking at the package you want to use to build a mobile app, there is no example and solution for custom types, (https://github.com/dreamsoftin/flutter_wordpress), but you could fork it, and extend it for specific custom post types. I will show you an example of how to do this (custom fields are excluded):

In flutter_wordpress/lib/constants.dart

add after line #10 const URL_POSTS = '$URL_WP_BASE/posts';

the line for an endpoint for your custom post. Say you have custom post book, you will add an endpoint books:

const URL_BOOKS = '$URL_WP_BASE/books';

see explanation about this and how to enable REST API for the custom post type here:

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-support

Then in the flutter_wordpress/lib/requests/ folder, find, clone and rename file:

params_post_list.dart to params_book_list.dart

And rename here class ParamsPostList to class ParamsBookList

in folder flutter_wordpress/lib/schemas/ find

post.dart copy and rename to book.dart

And rename here class Post to class Book

Then in the file flutter_wordpress/lib/flutter_wordpress.dart:

find line import 'schemas/post.dart'; and after that add line import 'schemas/book.dart';

find line export 'requests/params_post_list.dart'; and after that add line export 'requests/params_book_list.dart';

find line export 'schemas/post.dart'; and after that add line export 'schemas/book.dart';

Then find functions

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

Future<Post> _postBuilder()

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

copy these functions rename it and replace Post with Book (case sensitive)

note: find URL_POSTS in copied functions and rename to URL_BOOKS

Nikola Kirincic
  • 3,651
  • 1
  • 24
  • 28
  • Awesome ...!! Excellent.. Was not expecting this in-brief answer.. Thank you so much. I will add my code here for other people. BTW, can you tell me a hint about custom fields ? I am new in Flutter. And should I use this code or build the same with Chopper library ? Which one is better for WP as back end for android app ? – Mayanktaker Feb 22 '20 at 11:03
  • You should use this case only if you fork the `flutter_wordpress` project. Let me know if it works :) – Nikola Kirincic Feb 24 '20 at 08:37