diff --git a/drift/lib/src/runtime/types/sql_types.dart b/drift/lib/src/runtime/types/sql_types.dart index a1bca73f..14c2f16f 100644 --- a/drift/lib/src/runtime/types/sql_types.dart +++ b/drift/lib/src/runtime/types/sql_types.dart @@ -168,6 +168,10 @@ class BlobType extends SqlType { @override Uint8List? mapFromDatabaseResponse(dynamic response) { + if (response is String) { + final list = response.codeUnits; + return Uint8List.fromList(list); + } return response as Uint8List?; } diff --git a/drift/test/types/blob_test.dart b/drift/test/types/blob_test.dart index 7310d8ac..06dd9754 100644 --- a/drift/test/types/blob_test.dart +++ b/drift/test/types/blob_test.dart @@ -4,7 +4,7 @@ import 'package:drift/drift.dart'; import 'package:test/test.dart'; void main() { - test('maps without transormation', () { + test('maps without transformation', () { const type = BlobType(); final data = Uint8List.fromList(List.generate(256, (i) => i)); @@ -19,4 +19,14 @@ void main() { expect(type.mapToSqlConstant(data), equalsIgnoringCase("x'$hex'")); }); + + test('maps of string', () { + const chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'; + const type = BlobType(); + final data = List.generate(256, (i) => chars[i % chars.length]); + final dataString = data.join(); + final dataInt = data.map((e) => e.codeUnits[0]).toList(); + final dataUint8 = Uint8List.fromList(dataInt); + expect(type.mapFromDatabaseResponse(dataString), dataUint8); + }); }