convertColumn function Null safety

dynamic convertColumn(
  1. String columnName,
  2. List<PostgresColumn> columns,
  3. Map<String, dynamic> records,
  4. List<String> skipTypes
)

Converts the value of an individual column.

columnName The column that you want to convert columns All of the columns records The map of string values skipTypes An array of types that should not be converted

convertColumn('age', [{name: 'first_name', type: 'text'}, {name: 'age', type: 'int4'}], ['Paul', '33'], [])
=> 33
convertColumn('age', [{name: 'first_name', type: 'text'}, {name: 'age', type: 'int4'}], ['Paul', '33'], ['int4'])
=> "33"

Implementation

dynamic convertColumn(
  String columnName,
  List<PostgresColumn> columns,
  Map<String, dynamic> records,
  List<String> skipTypes,
) {
  final column = columns.firstWhereOrNull((x) => x.name == columnName);
  final columnValue = records[columnName];
  final columnValueStr = columnValue == null
      ? null
      : columnValue is String
          ? columnValue
          : columnValue.toString();

  if (column == null || skipTypes.contains(column.type)) {
    return noop(columnValueStr);
  } else {
    return convertCell(column.type, columnValueStr);
  }
}