read method

SqlType? read(
  1. BaseReferences<dynamic, $Table, dynamic> refs
)

Read the result of the computed field from the BaseReferences object

Example:

/// Filter users who are in an admin group
final inAdminGroup = db.managers.users.computed field((a) => a.group.isAdmin);
final users = db.managers.users.withComputedFields([inAdminGroup]).filter(inAdminGroup.filter(true)).get();

/// Aggregate the number of users in each group
final userCount = db.managers.group.computed field((a) => a.users((a) => a.id.count()));
final groups = db.managers.group.withComputedFields([userCount]).get();
for (final (group, refs) in groups) {
  final count = userCount.read(refs);
}

In this example:

  • The inAdminGroup computed field filters users directly in the database, ensuring that only users in the admin group are retrieved.
  • The userCount computed field aggregates the number of users in each group within the database, providing the count directly without needing to load all user data into your application.

Implementation

SqlType? read(BaseReferences<dynamic, $Table, dynamic> refs) {
  try {
    return refs.$_typedResult.read(_expression);
  } on ArgumentError {
    throw ArgumentError(
        'This computed field has not been added to the query. '
        'Use the .withComputedFields(...) method to add it to the query. ');
  }
}