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.
Add directus
to dependencies
in pubspec.yaml
and run pub get
or flutter pub get
.
More info can be found here.
Create instance and initialize. You must run .init()
for storage to be initialized. Othervise, there DirectusError
will be thrown.
import 'package:directus/directus.dart';
final sdk = await Directus('http://localhost:8055')
.init();
// ID must be `String` because Dart does not have union types.
final res = await sdk.items('users').readOne('someid');
print(res.data['name']);
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']));
final createdUser = await sdk.items('users').createOne({'name': 'Test'});
final createdUsers = await sdk.items('users').createMany([{'name': 'Test'}, {'name': 'Two'}]);
final updatedUser = await sdk.items('users').updateOne(data: {'name': 'Test'}, id: '55');
final updatedUsers = await sdk.items('users').updateMany(data: {'name': 'Test'}, ids: ['55']);
await sdk.items('users').deleteOne('55');
await sdk.items('users').deleteMany(['55']);
final isLoggedIn = sdk.auth.isLoggedIn;
await sdk.auth.login(email: 'test@example.com', password: 'password');
await sdk.logout();
// `currentUser` will be null if user is not logged in.
final user = await sdk.auth.currentUser?.read();
// `currentUser` will be null if user is not logged in.
final updatedUser = await sdk.auth.currentUser?.update({'name': 'Dart'});
// `fta` will be null if user is not logged in.
await sdk.auth.tfa?.enable('current-password');
// `fta` will be null if user is not logged in.
await sdk.auth.fta?.disable('otp');
await sdk.auth.forgottenPassword.request('email@example.com');
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');
final activity = await sdk.activity.readOne('some-id');
final activities = await sdk.activity.readMany(Query(limit: 10));
final comment = await sdk.activity.createComment(collection: 'posts',
item: 'some-id',
comment: 'Awesome post',
);
final updatedComment = await sdk.activity.updateComment(id: '50', comment: 'Awesome change!');
await sdk.activity.deleteComment('55');
Same methods as sdk.items(collection)
.
final collections = sdk.collections;
Same methods as sdk.items(collection)
.
final fields = sdk.fields;
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;
Same methods as sdk.items(collection)
.
final folders = sdk.folders;
Same methods as sdk.items(collection)
.
final permissions = sdk.permissions;
Same methods as sdk.items(collection)
.
final presets = sdk.presets;
Same methods as sdk.items(collection)
.
final relations = sdk.relations;
Same methods as sdk.items(collection)
.
final revisions = sdk.revisions;
Same methods as sdk.items(collection)
.
final roles = sdk.roles;
final pong = await sdk.server.ping();
final info = await sdk.server.info();
final oas = await sdk.server.oas();
Same methods as sdk.items(collection)
.
final settings = sdk.settings;
await sdk.users.invite(email: 'test@example.com', role: 'some-uuid');
await sdk.users.inviteMany(emails: ['test@example.com'], role: 'some-uuid');
await sdk.users.acceptInvite(token: 'fdasfsdafas', password: 'secret-password');
final randomString = await sdk.utils.randomString(15);
final hash = await sdk.utils.generateHash('value-to-hash');
final correctHash = await sdk.utils.verifyHash('Some value.', 'hashed-value');
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');
The key passed is the primary key of the revision you'd like to apply
await sdk.utils.revert('25');