搜索插件、代码仓与 APP 分类

插件详情v10.1.0

wordpress_client

一个用于与 WordPress REST API 交互的库。支持大多数常见端点以及对端点的所有 CRUD 操作。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
1370
喜欢数
38
Pub 分数
140

安装插件

flutter pub add wordpress_client

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个 Flutter/Dart 包,可实现与 WordPress REST API 的无缝交互。它支持常见端点和对文章、页面、评论等的完整 CRUD 操作。专为构建集成 WordPress 驱动网站的应用程序的开发者设计,可简化跨多个平台的数据检索和管理。该库轻量级、文档完善且持续维护。

适合场景

  • 获取博客文章和页面
  • 通过编程方式创建和更新内容
  • 管理评论和媒体
  • 构建自定义 WordPress 前端
  • 将 WordPress 作为无头 CMS 集成

主要特点

  • 核心端点的完整 CRUD 支持
  • 跨平台兼容性
  • 简单直观的 API
  • 支持通过令牌进行身份验证
  • 内置错误处理和响应解析

适合对象

  • 需要 WordPress 集成的 Flutter 应用
  • 无头CMS应用程序
  • 内容驱动的移动和网页应用
  • 使用 WordPress 作为后端的开发者

使用建议

  • 需要有效的 WordPress REST API 访问权限
  • 受保护端点需要进行身份验证设置
  • 仅限 WordPress REST API 功能
  • 不适合复杂的 WordPress 特定逻辑
  • 依赖外部WordPress服务器的稳定性

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

WordPress Client

Pub Version License Stars
Dart Flutter WordPress
The modern, strongly‑typed WordPress REST API client for Dart & Flutter — fast, flexible, and production‑ready.

🚀 Features

  • 📦 API discovery support.
  • ⏲️ Measures request completion time.
  • 📝 Supports all CRUD operations.
  • 🌐 Supports all common REST endpoints.
  • 🧩 GraphQL (WPGraphQL) support: client.graphql.query/mutate/raw() with auth.
  • 🎨 Custom Requests & Authorization systems.
  • 🔐 3 Popular authorization methods.
  • 🗄️ Third‑party Database integration.
  • 🔧 Middlewares for request & response operations.
  • 🎣 Events for preprocessing response.
  • 🚀 Execute requests in Parallel (with configurable error handling).
  • 🧠 Optional in‑memory caching for GET requests.
  • 🌊 Polling-based streaming for single-resource retrieve endpoints (works great with Riverpod's StreamProvider).

📦 Install

Add wordpress_client to your pubspec.yaml:

dependencies:
  wordpress_client: ^9.2.0

Then run flutter pub get or dart pub get.

🔧 Quick start

Import and initialize:

import 'package:wordpress_client/wordpress_client.dart';

final client = WordpressClient.forSite(
  siteUrl: Uri.parse('https://example.com'),
  // or: baseUrl via WordpressClient(baseUrl: Uri.parse('.../wp-json/wp/v2'))
);
🧩 GraphQL (WPGraphQL)

If your site uses the WPGraphQL plugin (canonical), you can send GraphQL operations using the same client and auth.

final client = WordpressClient.forSite(
  siteUrl: Uri.parse('https://example.com'),
  bootstrapper: (b) => b
    .withDefaultAuthorization(
      WordpressAuth.appPassword(user: 'user', appPassword: 'xxxx-xxxx'),
    )
    .build(),
);

// Auto-resolves https://example.com/graphql from the REST base.
final result = await client.graphql.query<List<Map<String, dynamic>>>(
  document: '''
    query Posts($limit: Int!) {
      posts(first: $limit) {
        nodes { id title }
      }
    }
  ''',
  variables: { 'limit': 5 },
  parseData: (data) {
    final nodes = (data['posts']?['nodes'] as List<dynamic>? ?? const [])
        .cast<Map<String, dynamic>>();
    return nodes;
  },
  requireAuth: false, // set true for restricted queries/mutations
);

switch (result) {
  case WordpressSuccessResponse():
    print(result.data);
  case WordpressFailureResponse():
    print('GraphQL error: ${result.error}');
}

// Custom endpoint path if your site exposes a different GraphQL path
client.graphql.setEndpointPath('/custom-graphql');

Add an auth quickly (helpers available):

final client = WordpressClient.forSite(
  siteUrl: Uri.parse('https://example.com'),
  bootstrapper: (b) => b
    .withDefaultAuthorization(
      // Built-in helpers
      WordpressAuth.appPassword(user: 'user', appPassword: 'xxxx-xxxx'),
      // or: WordpressAuth.usefulJwt(user: 'user', password: 'pass', device: 'device-id')
      // or: WordpressAuth.basicJwt(user: 'user', password: 'pass')
    )
    .build(),
);

Enable caching via middleware (optional):

final client = WordpressClient(
  baseUrl: Uri.parse('https://example.com/wp-json/wp/v2'),
  bootstrapper: (b) => b
    .withCache(
      ttl: const Duration(minutes: 5),               // optional, default 1 minute
      // cache: MyCustomCacheManager(),              // optional custom store
      // clearOnWrite: false,                        // keep cache after POST/PUT/PATCH/DELETE
    )
    .build(),
);

📝 Notes: applies to GET requests; default store is in-memory (custom stores supported); cache clears by default on writes.

📩 Send a request

Retrieve 20 recent posts in ascending order:

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

final response = await client.posts.list(request);

// Dart 3 pattern matching
switch (response) {
  case WordpressSuccessResponse():
    final data = response.data; // List<Post>
    break;
  case WordpressFailureResponse():
    final error = response.error; // WordpressError
    break;
}

// Or use helpers
final posts = (await client.posts.list(ListPostRequest(perPage: 20))).map(
  onSuccess: (s) => s.data,
  onFailure: (f) => <Post>[],
);

final justPosts = (await client.posts.list(ListPostRequest(perPage: 20))).dataOrThrow();

Convenience extensions are available under client.<interface>.extensions for quick lookups and pagination:

final post = await client.posts.extensions.getById(123);
final allMedia = await client.media.extensions.listAll(perPage: 100);

📚 Advanced docs (Wiki)

Deep-dives and more examples live in the Wiki:

🤝 Feedback & Contributing

  • 🐛 For bugs or feature requests, use the issue tracker.
  • 💡 Contributions are always appreciated. PRs are welcome!

Contributor tips (scripts, release process, consistency guards) are documented in the Wiki.

📄 License

This project is MIT licensed.


If you find this package helpful, consider supporting the development:

Buy Me A Coffee

所属分类

依赖

依赖清单 8 项

源码仓库

使用此插件的项目

暂无相关数据。