如何解决使用来自 id 的对象填充冻结的模型
背景
我正在使用 sembast
将模型存储在本地存储中。这里的逻辑是,每个 Sprint
将有 5 个 Day
对象。我没有将整个对象存储在 Sprint
对象中,而是存储这些 Day
对象的 ID。请记住,所有 Sprint
和 Day
都将分别存储在 sprint
和 day
存储中的本地存储中。
问题
我想使用存储的 Task
从 Sprint
对象访问 taskIds
对象。这就是为什么我想用 Sprint
对象填充 Task
对象。
@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
对象上有一个Sprint
方法,可以在onModelReady
方法中调用它,我认为它添加了大量代码可以防止。
Future<void> onModelReady() async {
await currentSprint.populateWithDays();
}
void useDays() {
for(final day in sprint.days) { // here `days` is different than `dayIds`
print(day.dateTime);
}
}
当调用 Sprint
时,我可以通过修改 Day
模型来保存与 dayIds
中的 days
关联的 populateWithDays
对象列表。但我不想将 dayIds
和 days
存储在同一个对象中。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。