read method

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

Read the result of the computed field from the a 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

DartType? read(BaseReferences<dynamic, $Table, dynamic> refs) {
  final dartType = refs.$_typedResult.read(_expression);
  if (dartType == null) {
    return null;
  }
  return _expression.converter.fromSql(dartType);
}