0

I am trying to rewrite my code to make is null safety.

  PostgreSQLResult getResultFromCache(sqlQuery) {
    for (var req in requestsCacheList) {
      if(req.requestHash == sqlQuery.hashCode) {
        return req.requestResult;
      }
    }
    // var p = PostgreSQLResult(); // what should be here? Implementing PostgreSQLResult?
    // return p;
  }

I should to return here PostgreSQLResult instead of null. I looked sources what PostgreSQLResult is:

/// A single row of a query result.
///
/// Column values can be accessed through the `[]` operator.
abstract class PostgreSQLResultRow implements List {
  List<ColumnDescription> get columnDescriptions;

  /// Returns a two-level map that on the first level contains the resolved
  /// table name, and on the second level the column name (or its alias).
  Map<String, Map<String, dynamic>> toTableColumnMap();

  /// Returns a single-level map that maps the column name (or its alias) to the
  /// value returned on that position.
  Map<String, dynamic> toColumnMap();
}

/// The query result.
///
/// Rows can be accessed through the `[]` operator.
abstract class PostgreSQLResult implements List<PostgreSQLResultRow> {
  /// How many rows did this query affect?
  int get affectedRowCount;
  List<ColumnDescription> get columnDescriptions;
}

Could you show me how should be look empty result PostgreSQLResult set?

Dmitry Bubnenkov
  • 9,415
  • 19
  • 85
  • 145
  • Null-safety doesn't mean never using `null`. There are times when using `null` is appropriate, such as when you have no result to return. Also compare to `Map.operator []`. Just make `getResultFromCache` return a `PostgreSQLResult?` instead. – jamesdlin Aug 31 '21 at 20:12
  • You alternatively could create your own class that derives from `PostgreSQLResult` and overrides its methods to return `0` for `affectedRowCount` and `[]` for `columnDescriptions`, but in general you might want to disintguish between a cached empty result and no cached result. – jamesdlin Aug 31 '21 at 20:23

0 Answers0