drift
drift copied to clipboard
Error with type inference in drift 2.0
Hello Simon, I've just updated to use drift 2.0 and I encountered with a very curious failing test in my test suite.
The following test in the integration_tests/select_integration_test.dart file fails with the following error:
Invalid argument(s): Could not find a matching SQL type for Never
DriftSqlType.forType
package:drift/…/types/mapping.dart:253
QueryRow.readNullable
package:drift/…/select/custom_select.dart:97
QueryRow.read
package:drift/…/select/custom_select.dart:82
main.getCategoryIdByDescription
test/integration_tests/select_integration_test.dart:41
===== asynchronous gap ===========================
main.<fn>
Future<int?> getCategoryIdByDescription(TodoDb appDatabase, String description) async {
const q = "SELECT id FROM categories WHERE desc = ?";
final row = await appDatabase.customSelect(
q,
variables: [Variable<String>(description)],
).getSingleOrNull();
return row?.read("id");
}
test('failing', () async {
final categoryDescription = 'category description';
await db.categories.insertOne(CategoriesCompanion.insert(description: categoryDescription));
// Search the category we just inserted
final categoryId = await getCategoryIdByDescription(db, categoryDescription);
expect(categoryId != null, true);
});
It seems to be due to the type inference in the line
return row?.read("id");
If we change that into the following it works ok
final id = row?.read<int>("id");
return id;
I don't know if this is fixable in here or if it is a bug in the Dart SDK, but this wasn't failing in previous versions of drift.
Let's hope this has a solution, since this would be very hard to catch in existing code as it only fails in runtime.