assocFirst property

  1. @override
Map<String, String?>? get assocFirst
override

Returns the first row as an associative array, or null if no rows exist. This is convenient for queries that are expected to return a single row, such as SELECT queries with LIMIT 1 or aggregate functions. Example:

var firstRow = result.assocFirst;
if (firstRow != null) {
  print('User found: ${firstRow['name']}');
} else {
  print('No user found');
}

Implementation

@override
Map<String, String?>? get assocFirst {
  if (rows.isEmpty) {
    return null;
  }
  var row = <String, String?>{};
  for (var i = 0; i < numFields; i++) {
    row[resultSet.columnNames[i]] = rows.first[i]?.toString();
  }
  return row;
}