-
Notifications
You must be signed in to change notification settings - Fork 194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for embedded objects #565
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:example/date_time_converter.dart'; | ||
import 'package:example/task.dart'; | ||
import 'package:example/task_dao.dart'; | ||
import 'package:example/timestamp.dart'; | ||
import 'package:floor/floor.dart'; | ||
import 'package:sqflite/sqflite.dart' as sqflite; | ||
|
||
part 'database.g.dart'; | ||
|
||
@Database(version: 1, entities: [Task]) | ||
@TypeConverters([DateTimeConverter]) | ||
@Database(version: 1, entities: [Task], embeds: [Timestamp]) | ||
abstract class FlutterDatabase extends FloorDatabase { | ||
TaskDao get taskDao; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import 'package:floor/floor.dart'; | ||
|
||
class DateTimeConverter extends TypeConverter<DateTime, int> { | ||
@override | ||
DateTime decode(int databaseValue) => DateTime.fromMillisecondsSinceEpoch(databaseValue); | ||
|
||
@override | ||
int encode(DateTime value) => value.millisecondsSinceEpoch; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import 'package:example/timestamp.dart'; | ||
import 'package:floor/floor.dart'; | ||
|
||
@entity | ||
|
@@ -7,7 +8,10 @@ class Task { | |
|
||
final String message; | ||
|
||
Task(this.id, this.message); | ||
// @ColumnInfo(name: '') | ||
final Timestamp timestamp; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm personally in favor of having There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hello sir! I don't have a background in Android/Java so I was not aware of the room approach. I think that annotating the class as an embedded type is easier as we need to annotate it just once, instead of annotating every time we declare it inside an entity class. For finely adjusting, as we have just the If you prefer the room approach I can modify it, just tell me :) |
||
|
||
Task(this.id, this.message, this.timestamp); | ||
|
||
@override | ||
bool operator ==(Object other) => | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import 'package:floor/floor.dart'; | ||
|
||
@Embed() | ||
class Timestamp { | ||
@ColumnInfo(name: 'created_at') | ||
final DateTime createdAt; | ||
|
||
@ColumnInfo(name: 'updated_at') | ||
final DateTime updatedAt; | ||
|
||
Timestamp({required this.createdAt, required this.updatedAt}); | ||
|
||
@override | ||
bool operator ==(Object other) => | ||
identical(this, other) || | ||
other is Timestamp && | ||
runtimeType == other.runtimeType && | ||
createdAt == other.createdAt && | ||
updatedAt == other.updatedAt; | ||
|
||
@override | ||
int get hashCode => createdAt.hashCode ^ updatedAt.hashCode; | ||
|
||
@override | ||
String toString() { | ||
return 'Timestamp{createdAt: $createdAt, updatedAt: $updatedAt}'; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Embed { | ||
const Embed(); | ||
} | ||
|
||
const embed = Embed(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to include the embeds here? doesn't it suffice to annotate the usage in the entities/views?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to include if we annotate the embedded class, if we annotate the variable like room then we don't need to include here.