directus 0.11.0 copy "directus: ^0.11.0" to clipboard
directus: ^0.11.0 copied to clipboard

The unofficial SDK for Directus, provides REST API and authentication. Requires Flutter.

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. 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 .
copied to clipboard

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();
copied to clipboard

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();
copied to clipboard

Examples #

Singleton #

import 'package:directus/directus.dart';

await DirectusSingleton.init('http://localhost:8055')
final sdk = DirectusSingleton.instance;
copied to clipboard

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']);
copied to clipboard

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']));
copied to clipboard

Create Single Item

final createdUser = await sdk.items('users').createOne({'name': 'Test'});
copied to clipboard

Create Many Items

final createdUsers = await sdk.items('users').createMany([{'name': 'Test'}, {'name': 'Two'}]);
copied to clipboard

Update Item by ID

final updatedUser = await sdk.items('users').updateOne(data: {'name': 'Test'}, id: '55');
copied to clipboard

Update Many Items

final updatedUsers = await sdk.items('users').updateMany(data: {'name': 'Test'}, ids: ['55']);
copied to clipboard

Delete Item by ID

await sdk.items('users').deleteOne('55');
copied to clipboard

Delete Many Items

await sdk.items('users').deleteMany(['55']);
copied to clipboard

Auth #

Is User Logged In

final isLoggedIn = sdk.auth.isLoggedIn;
copied to clipboard

Login

await sdk.auth.login(email: 'test@example.com', password: 'password');
copied to clipboard

Logout

await sdk.logout();
copied to clipboard

Get Current User

// `currentUser` will be null if user is not logged in.
final user = await sdk.auth.currentUser?.read();
copied to clipboard

Update Current User

// `currentUser` will be null if user is not logged in.
final updatedUser = await sdk.auth.currentUser?.update({'name': 'Dart'});

copied to clipboard

Enable Two Factor Authentication

// `fta` will be null if user is not logged in.
await sdk.auth.tfa?.enable('current-password');
copied to clipboard

Disable 2FA

// `fta` will be null if user is not logged in.
await sdk.auth.fta?.disable('otp');
copied to clipboard

Request a Password Reset

await sdk.auth.forgottenPassword.request('email@example.com');
copied to clipboard

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');
copied to clipboard

Activity #

Read Activity

final activity = await sdk.activity.readOne('some-id');
final activities = await sdk.activity.readMany(Query(limit: 10));
copied to clipboard

Comment


final comment = await sdk.activity.createComment(collection: 'posts',
                  item: 'some-id',
                  comment: 'Awesome post',
                );
copied to clipboard

Change Comment

final updatedComment = await sdk.activity.updateComment(id: '50', comment: 'Awesome change!');
copied to clipboard

Remove comment

await sdk.activity.deleteComment('55');
copied to clipboard

Collections #

Same methods as sdk.items(collection).

final collections = sdk.collections;
copied to clipboard

Fields #

Same methods as sdk.items(collection).

final fields = sdk.fields;
copied to clipboard

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;
copied to clipboard

Folders #

Same methods as sdk.items(collection).

final folders = sdk.folders;
copied to clipboard

Permissions #

Same methods as sdk.items(collection).

final permissions = sdk.permissions;
copied to clipboard

Presets #

Same methods as sdk.items(collection).

final presets = sdk.presets;
copied to clipboard

Relations #

Same methods as sdk.items(collection).

final relations = sdk.relations;
copied to clipboard

Revisions #

Same methods as sdk.items(collection).

final revisions = sdk.revisions;
copied to clipboard

Roles #

Same methods as sdk.items(collection).

final roles = sdk.roles;
copied to clipboard

Server #

Ping the Server

final pong = await sdk.server.ping();
copied to clipboard

Get Server/Project Info

final info = await sdk.server.info();
copied to clipboard

Get the API Spec in OAS Format

final oas = await sdk.server.oas();
copied to clipboard

Settings #

Same methods as sdk.items(collection).

  final settings = sdk.settings;
copied to clipboard

Users #

Invite a New User

await sdk.users.invite(email: 'test@example.com', role: 'some-uuid');
copied to clipboard

Invite multiple users

await sdk.users.inviteMany(emails: ['test@example.com'], role: 'some-uuid');
copied to clipboard

Accept a User Invite

await sdk.users.acceptInvite(token: 'some-token', password: 'secret-password');
copied to clipboard

Utils #

Get a Random String

final randomString = await sdk.utils.randomString(15);
copied to clipboard

Generate a Hash for a Given Value

final hash = await sdk.utils.generateHash('value-to-hash');
copied to clipboard

Verify if a Hash is Valid

final correctHash = await sdk.utils.verifyHash('Some value.', 'hashed-value');
copied to clipboard

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');
copied to clipboard

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');
copied to clipboard
39
likes
160
points
516
downloads

Publisher

verified publisherdirectus.io

Weekly Downloads

2024.09.14 - 2025.03.29

The unofficial SDK for Directus, provides REST API and authentication. Requires Flutter.

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

directus_core, flutter, shared_preferences

More

Packages that depend on directus