directus-dart icon indicating copy to clipboard operation
directus-dart copied to clipboard

How to get custom user fields?

Open DigNZ opened this issue 2 years ago • 2 comments

I'm digging down a rabbit hole:

  1. Subclass DirectusUser to have a type with additional fields
  2. Add an extension to CurrentUser similar to read() readCustomUser() <- didn't really work
  3. Add a CustomUserConverter() <- can't inject this into CurrentUser.read
  4. Implement the client call directly and skip the currentUser accessor <- works but seems smelly.

Is there a way to get a user object with custom fields? If not there should be. If yes can it be documented please.

DigNZ avatar Aug 16 '22 23:08 DigNZ

I do not think there is a way to do it currently. Does Directus have support to adding custom fields in system tables?

apstanisic avatar Aug 22 '22 12:08 apstanisic

For 3 option, it is possible to customize converter with sdk.users.converter = MyConverter();. Only make sure that return value extends DirectusUser or Dart will throw an exception.

Hi, I played around a little, and this is simplest solution I could think off. I know it's not ideal, if anybody has time I am open to more proper solutions.

  final sdk = await getSdk();
  final user = DirectusUser(
      email: "[email protected]",
      password: "password",
// this values are same as columns, they should not be camel cased
      rawValues: {"test_value": "RawValue"});
  final res = await sdk.users.createOne(user);
  print(res.data.rawValues['test_value']);

apstanisic avatar Jan 09 '23 22:01 apstanisic