コンテキストモデルをローカル ストレージに保存するため
に使用しています。sembastここでのロジックは、すべてSprintに 5 つDayのオブジェクトがあるということです。オブジェクト全体をオブジェクトに格納する代わりにSprint、それらのオブジェクトの ID を格納していDayます。すべてSprintとDayは、それぞれローカル ストレージのsprintとdayストアに格納されることに注意してください。
問題stored を使用して objectTaskからオブジェクト
にアクセスしたい。そのため、オブジェクトにオブジェクトを設定したいと考えています。SprinttaskIdsSprintTask
Sprint次の構造を持つ凍結モデルがあります。
@freezed
class Sprint with _$Sprint {
factory Sprint({
required String id,
required String title,
required DateTime startDateTime,
required List<String> dayIds,
}) = _Sprint;
factory Sprint.fromJson(Map<String, dynamic> json) =>
_$SprintFromJson(json);
}
およびDay構造を持つモデル:
@freezed
class Day with _$Day {
factory Day({
required String id,
required DateTime dateTime,
}) = _Day;
factory Day.fromJson(Map<String, dynamic> json) =>
_$DayFromJson(json);
}
daysビューモデルのロード時に変数を使用しているアプローチ
List<Day> days = []
Future<void> onModelReady() async {
for(final dayId in currentSprint.dayIds) {
days.add(await _dayController.fetchDayById(dayId))
}
}
// use `days` variable
しかし、メソッドで呼び出すことができるオブジェクトにpopulateWithDaysメソッドがあれば防ぐことができる豊富なコードを追加すると思います。SprintonModelReady
Future<void> onModelReady() async {
await currentSprint.populateWithDays();
}
void useDays() {
for(final day in sprint.days) { // here `days` is different than `dayIds`
print(day.dateTime);
}
}
が呼び出されたときに、プロパティに関連付けられている s オブジェクトのSprintリストを保持するようにモデルを変更することで、これを行うことができます。しかし、同じオブジェクトに保存したくありません。DaydayIdsdayspopulateWithDaysdayIdsdays