Custom Row Class Example #3120
-
I love the idea that you can inject custom row classes to drift. I tried the example on the doc page https://drift.simonbinder.eu/docs/advanced-features/custom_row_classes/ but unfortunately it doesnt work as expected either the builder fails with:
or if i delete the database.g.dart before i get a broken database.g.dart @override
Set<GeneratedColumn> get $primaryKey => {id};
@override
User map(Map<String, dynamic> data, {String? tablePrefix}) {
final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : '';
return User( //<--- missing birthdate
id: attachedDatabase.typeMapping
.read(DriftSqlType.int, data['${effectivePrefix}id'])!,
name: attachedDatabase.typeMapping
.read(DriftSqlType.string, data['${effectivePrefix}name'])!,
);
} DatabaseClass: import 'dart:io';
import 'package:drift/drift.dart';
import 'package:drift_flutter/drift_flutter.dart';
import 'package:path/path.dart' as p;
import 'package:daily_moments/models/database/create/user.dart';
import 'package:path_provider/path_provider.dart';
part 'database.g.dart';
@DriftDatabase(
include: {'create/create_db.drift'}
)
class AppDatabase extends _$AppDatabase {
static String databaseName = 'dailyMoments';
AppDatabase() : super(_openConnection());
@override
int get schemaVersion => 1;
static QueryExecutor _openConnection() {
// `driftDatabase` from `package:drift_flutter` stores the database in
// `getApplicationDocumentsDirectory()`.
return driftDatabase(name: AppDatabase.databaseName);
}
static Future<void> deleteDatabase() async {
// `driftDatabase` from `package:drift_flutter` stores the database in
// `getApplicationDocumentsDirectory()`.
Directory folder = await getApplicationDocumentsDirectory();
print(folder.listSync());
File dbFile = File(p.join(folder.path, AppDatabase.databaseName));
await dbFile.delete();
}
}
Drift file: import 'user.dart'; -- or what the Dart file is called
CREATE TABLE users(
id INTEGER NOT NULL PRIMARY KEY,
name TEXT NOT NULL,
birth_date DATETIME NOT NULL
) WITH User;
I also tried Model: class User {
final int id;
final String name;
final DateTime birthday;
User({required this.id, required this.name, required this.birthday});
} So first of all I have a question:
And the other question:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Okay I analyzed it further and made an flutter example of it. ~~What works: ~~
I think I found the error. It was a typo with AND about the path ... I think it fixes it if I import the models in the |
Beta Was this translation helpful? Give feedback.
Okay I analyzed it further and made an flutter example of it.
~~What works: ~~
* use CreationModels likeUsers
* use drift files without Rowclass modelswhat does not work:* use drift files withWITH
statement and custom rowclassesCan somebody explain this or is this a bug ?I think I found the error. It was a typo with
birth_date
.AND about the path ...
I think it fixes it if I import the models in the
database.dart
file, causedatabase.g.dart
is a part from it, right ?