convertStringValueToProvidedType<T> method

T? convertStringValueToProvidedType<T>(
  1. dynamic value, [
  2. int? columnLength
])

Converte um valor (normalmente lido do MySQL em formato string ou binário) para o tipo Dart desejado T.

value: valor lido (pode ser uma string ou Uint8List, dependendo da coluna). columnLength: (opcional) o tamanho da coluna, usado para decisões de conversão.

Se o valor for nulo, retorna null. Se T for Uint8List e o valor já for do tipo Uint8List, retorna-o diretamente. Se T for String ou dynamic, retorna o valor como está. Para tipos numéricos, bool ou DateTime, o método tenta converter a string utilizando as funções parse correspondentes.

Lança MySQLProtocolException se não for possível converter.

Implementation

T? convertStringValueToProvidedType<T>(dynamic value, [int? columnLength]) {
  if (value == null) {
    return null;
  }

  if (T == dynamic || _matchesType<T, Object>()) {
    return value as T;
  }

  if (_matchesType<T, Uint8List>()) {
    if (value is Uint8List) {
      return value as T;
    }
    throw MySQLProtocolException(
      'Cannot convert MySQL type $_value (${value.runtimeType}) to requested type $T',
    );
  }

  if (value is! String) {
    throw MySQLProtocolException(
      'Cannot convert MySQL type $_value (${value.runtimeType}) to requested type $T',
    );
  }

  return _convertStringToType<T>(value, columnLength);
}