12.02 Flutter已經支持WordPress框架 Flutter可以做web開發

今天無意間發現一個有意思的插件,可以用Flutter來獲取WordPress的接口。WordPress是什麼?

WordPress是一個以PHP和MySQL為平臺的自由開源的博客軟件和內容管理系統。WordPress具有插件架構和模板系統。截至2018年4月,排名前1000萬的網站中超過30.6%使用WordPress。WordPress是最受歡迎的網站內容管理系統。WordPress是當前因特網上最流行的博客系統。

一般WordPress的後臺都是用php java 等這些語言來獲取數據,這次Flutter也插入進來了,所以Flutter不止Android,iOS 實現語言,它要做的事情還有很多很多,前方的道路還有很遠,戰鬥從來沒有停止過,只要你感想,它就敢做,加油。

本頭條核心宗旨

歡迎來到「技術剛剛好」作者,「技術剛剛好」是個人維護,每天至少更新一篇Flutter技術文章,實時為大家播報Flutter最新消息。如果你剛好也在關注Flutter這門技術,那就跟我一起學習進步吧,你的贊,收藏,轉發是對我個人最大的支持,維護不易,歡迎關注。

技術剛剛好經歷

近幾年,移動端跨平臺開發技術層出不窮,從Facebook家的ReactNative,到阿里家WEEX,前端技術在移動端跨平臺開發中大展身手,技術剛剛好作為一名Android開發,經歷了從Reactjs到Vuejs的不斷學習。而在2018年,我們的主角變成了Flutter,這是Goolge開源的一個移動端跨平臺解決方案,可以快速開發精美的移動App。希望跟大家一起學習,一起進步!

本文核心要點

今天就把這個插件分享給大家,就當是一個練手學習的項目,如果大家感興趣可以進去看看。

插件地址:https://pub.dev/packages/flutter_wordpress

接入方式:

Flutter Wordpress #

該庫使用WordPress REST API V2為您的應用程序提供了一種與WordPress網站進行交互的方式。

要求為了進行身份驗證和使用管理員級別的REST API,您需要使用WordPress網站中兩個流行的身份驗證插件之一:

  1. 應用密碼
  2. WP REST API的JWT身份驗證 (推薦)

1.導入庫號

https://pub.dartlang.org/packages/flutter_wordpress

import 'package:flutter_wordpress/flutter_wordpress.dart' as wp;

2.實例化WordPress類

wp.WordPress wordPress;

// adminName and adminKey is needed only for admin level APIs
wordPress = wp.WordPress(

baseUrl: 'http://localhost',
authenticator: wp.WordPressAuthenticator.JWT,
adminName: '',
adminKey: '',
);

3.驗證用戶編號

Future<wp.user> response = wordPress.authenticateUser(
username: 'ChiefEditor',
password: 'chiefeditor@123',
);

response.then((user) {
createPost(user);
}).catchError((err) {
print('Failed to fetch user: $err');
});
/<wp.user>

4.獲取帖子

Future<list>> posts = wordPress.fetchPosts(
params: wp.ParamsPostList(
context: wp.WordPressContext.view,
pageNum: 1,
perPage: 20,
order: wp.Order.desc,
orderBy: wp.PostsOrderBy.date,
),
fetchAuthor: true,
fetchFeaturedMedia: true,
fetchComments: true,
);
/<list>

5.獲取用戶

Future<list>> users = wordPress.fetchUsers(
params: wp.ParamsUserList(
context: wp.WordPressContext.view,
pageNum: 1,
perPage: 30,
order: wp.Order.asc,
orderBy: wp.UsersOrderBy.name,
role: wp.UserRole.subscriber,
),
);

/<list>

6.獲取評論

Future<list>> comments = wordPress.fetchComments(
params: wp.ParamsCommentList(
context: wp.WordPressContext.view,
pageNum: 1,
perPage: 30,
includePostIDs: [1],
),
);
/<list>

7.創建帖子編號

void createPost(wp.User user) {
final post = wordPress.createPost(
post: new wp.Post(
title: 'First post as a Chief Editor',
content: 'Blah! blah! blah!',
excerpt: 'Discussion about blah!',
author: user.id,
commentStatus: wp.PostCommentStatus.open,
pingStatus: wp.PostPingStatus.closed,
status: wp.PostPageStatus.publish,
format: wp.PostFormat.standard,
sticky: true,
),
);

post.then((p) {
print('Post created successfully with ID: ${p.id}');
postComment(user, p);
}).catchError((err) {
print('Failed to create post: $err');
});
}

8.發表評論

void postComment(wp.User user, wp.Post post) {
final comment = wordPress.createComment(
comment: new wp.Comment(
author: user.id,
post: post.id,
content: "First!",
parent: 0,

),
);

comment.then((c) {
print('Comment successfully posted with ID: ${c.id}');
}).catchError((err) {
print('Failed to comment: $err');
});
}

總結

還是那句話,程序員不只是敲代碼,還要天天學習,不管你是20歲,30歲,40歲,只要是程序員你就得不停學習,不停前進。

謝謝觀看技術剛剛好的文章技術剛剛好是個人維護,每天至少更新一篇Flutter技術文章,實時為大家播報Flutter最新消息。如果你剛好也在關注Flutter這門技術,那就跟我一起學習進步吧,你的贊,收藏,轉發是對我個人最大的支持,維護不易,歡迎關注。


分享到:


相關文章: