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

插件详情v0.11.0

directus

Directus 的非官方 SDK,提供 REST API 和身份验证。需要 Flutter。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
224
喜欢数
40
Pub 分数
160

安装插件

flutter pub add directus

插件介绍

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

适用于 Directus 的非官方 Dart SDK 使 Flutter 应用能够与 Directus 的 REST API 交互,并无缝处理身份验证。它提供了一种结构化的方式来通过类型安全的接口管理数据、用户和权限。专为使用 Flutter 的开发者设计,支持所有主要平台,包括 Android、iOS、Web、macOS、Windows 和 Linux。该 SDK 简化了与 Directus 作为无头 CMS 或后端服务的集成,减少了常见操作(如查询集合、上传文件和管理会话)的样板代码。

适合场景

  • 将 Flutter 应用与 Directus 作为后端集成
  • 通过无头CMS管理内容
  • 用户身份验证和会话处理
  • 实时数据同步
  • 文件上传和管理
  • 构建管理员仪表板

主要特点

  • Directus 的 REST API 客户端
  • 身份验证和令牌管理
  • 类型安全的数据模型
  • 跨平台支持
  • 使用 Flutter 轻松设置
  • 对文件操作的支持

适合对象

  • Flutter 开发人员使用 Directus 构建应用
  • 使用 Directus 作为后端的团队
  • 需要内容管理的项目
  • 需要安全用户认证的应用程序
  • 寻求轻量级后端集成的开发者

使用建议

  • 非官方 SDK – 不由 Directus 团队维护
  • 仅限 REST API(无实时订阅)
  • 需要手动处理错误情况
  • 依赖外部包的更新
  • 无内置缓存或离线支持

原始 README

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

Directus SDK for Dart/Flutter

Unofficial Directus SDK for Dart/Flutter that provides APIs for reading, creating, updating and deleting user and system data, authentication, and access to activity. This package is port of SDK for JS from [here](This package is port of SDK for JS from here. Most methods are same as in JS, but there are some differences because of Dart type system.

Installation

Add directus to dependencies in pubspec.yaml and run pub get or flutter pub get. More info can be found here.

Contributing

Run code bellow before committing. Writing tests is welcomed, but not required.

flutter test
flutter format .

Getting started

Create instance and initialize. You must run .init() for storage to be initialized. Otherwise, there DirectusError will be thrown.

directus

This package requires Flutter since it's using shared_preferences for persisting data.

import 'package:directus/directus.dart';

final sdk = await Directus('http://localhost:8055')
                    .init();
directus_core

This package does not require Flutter, but it does not know how to store data, so you have to pass it your custom storage that extends DirectusStorage. We provide memory storage, that holds your data in memory while app is live.

import 'package:directus_core/directus_core.dart';

// Provide your custom storage
final sdk = await DirectusCore('http://localhost:8055', storage: MemoryStorage())
                    .init();

Examples

Singleton
import 'package:directus/directus.dart';

await DirectusSingleton.init('http://localhost:8055')
final sdk = DirectusSingleton.instance;
Using collections
Get Item by ID:
// ID must be `String` because Dart does not have union types.
final res = await sdk.items('users').readOne('someId');
print(res.data['name']);
Get Many Items
final users =  await sdk.items('users').readMany(Query(limit: 5, offset: 5));
users.data.forEach((user) => print(user['name']));

final firstThreeUsers = await DirectusSdk().items('users').readMany(
      filter: Filters({'id': Filter.isIn(['1', '2'])})
    );
firstThreeUsers.data.forEach((user) => print(user['name']));
Create Single Item
final createdUser = await sdk.items('users').createOne({'name': 'Test'});
Create Many Items
final createdUsers = await sdk.items('users').createMany([{'name': 'Test'}, {'name': 'Two'}]);
Update Item by ID
final updatedUser = await sdk.items('users').updateOne(data: {'name': 'Test'}, id: '55');
Update Many Items
final updatedUsers = await sdk.items('users').updateMany(data: {'name': 'Test'}, ids: ['55']);
Delete Item by ID
await sdk.items('users').deleteOne('55');
Delete Many Items
await sdk.items('users').deleteMany(['55']);

Auth
Is User Logged In
final isLoggedIn = sdk.auth.isLoggedIn;
Login
await sdk.auth.login(email: 'test@example.com', password: 'password');
Logout
await sdk.logout();
Get Current User
// `currentUser` will be null if user is not logged in.
final user = await sdk.auth.currentUser?.read();
Update Current User
// `currentUser` will be null if user is not logged in.
final updatedUser = await sdk.auth.currentUser?.update({'name': 'Dart'});

Enable Two Factor Authentication
// `fta` will be null if user is not logged in.
await sdk.auth.tfa?.enable('current-password');
Disable 2FA
// `fta` will be null if user is not logged in.
await sdk.auth.fta?.disable('otp');
Request a Password Reset
await sdk.auth.forgottenPassword.request('email@example.com');
Reset a Password

the token passed in the first parameter is sent in an email to the user when using request().

await sdk.auth.forgottenPassword.reset(token: 'some-token', password: 'new-password');

Activity
Read Activity
final activity = await sdk.activity.readOne('some-id');
final activities = await sdk.activity.readMany(Query(limit: 10));
Comment

final comment = await sdk.activity.createComment(collection: 'posts',
                  item: 'some-id',
                  comment: 'Awesome post',
                );
Change Comment
final updatedComment = await sdk.activity.updateComment(id: '50', comment: 'Awesome change!');
Remove comment
await sdk.activity.deleteComment('55');

Collections

Same methods as sdk.items(collection).

final collections = sdk.collections;

Fields

Same methods as sdk.items(collection).

final fields = sdk.fields;

Files

Methods readOne, readMany, deleteOne, deleteMany are the same as in items(collection). There is currently experimental uploadFile method that is still not stable. There are not updateOne, updateMany, createOne and createMany.

final files = sdk.files;

Folders

Same methods as sdk.items(collection).

final folders = sdk.folders;

Permissions

Same methods as sdk.items(collection).

final permissions = sdk.permissions;

Presets

Same methods as sdk.items(collection).

final presets = sdk.presets;

Relations

Same methods as sdk.items(collection).

final relations = sdk.relations;

Revisions

Same methods as sdk.items(collection).

final revisions = sdk.revisions;

Roles

Same methods as sdk.items(collection).

final roles = sdk.roles;

Server
Ping the Server
final pong = await sdk.server.ping();
Get Server/Project Info
final info = await sdk.server.info();
Get the API Spec in OAS Format
final oas = await sdk.server.oas();
Settings

Same methods as sdk.items(collection).

  final settings = sdk.settings;

Users
Invite a New User
await sdk.users.invite(email: 'test@example.com', role: 'some-uuid');
Invite multiple users
await sdk.users.inviteMany(emails: ['test@example.com'], role: 'some-uuid');
Accept a User Invite
await sdk.users.acceptInvite(token: 'some-token', password: 'secret-password');

Utils
Get a Random String
final randomString = await sdk.utils.randomString(15);
Generate a Hash for a Given Value
final hash = await sdk.utils.generateHash('value-to-hash');
Verify if a Hash is Valid
final correctHash = await sdk.utils.verifyHash('Some value.', 'hashed-value');
Sort Items in a Collection

This will move item 5 to the position of item 10, and move everything in between one "slot" up

await sdk.utils.sort(collection: 'users', itemPk: '5', toPk: '10');
Revert to a Previous Revision

The key passed is the primary key of the revision you'd like to apply

await sdk.utils.revert('25');

所属分类

依赖

依赖清单 2 项

源码仓库

使用此插件的项目

暂无相关数据。