搜尋套件、儲存庫與 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 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。