flutter - 使用来自 id 的对象填充冻结模型
问题描述
我sembast
用来将模型存储在本地存储中的上下文。这里的逻辑是,每个Sprint
都会有 5 个Day
对象。Sprint
我没有将整个对象存储在对象中,而是存储这些Day
对象的 ID。请记住,所有Sprint
和Day
都将分别存储在本地存储中sprint
和day
存储中。
问题
我想使用存储Task
的对象访问对象。这就是为什么,我想用对象填充对象。Sprint
taskIds
Sprint
Task
我有一个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
但是我认为它添加了丰富的代码,如果对象上有方法可以防止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
模型来保存与in属性Day
关联的 s 对象列表,当被调用时。但我不想将和存储在同一个对象中。dayIds
days
populateWithDays
dayIds
days
解决方案
推荐阅读
- java - getIntent().getExtra() is returing null value
- sql - SQL 将行合并到新列中
- ruby-on-rails - 如何直接将图像从 heroku 控制台上传到我的 s3 存储桶
- reactjs - 未捕获的类型错误:无法解构“未定义”的属性“getFirestore”,因为它未定义
- python-3.x - 在while循环之前检查iterable是否为空
- spring - Liquibase 优化更新日志问题的完整性(ValidationFailedException: Validation Failed)
- sql - 基于特定表值的 SQL 连接选择
- python - 如何在熊猫 df.query() 中使用循环变量
- python - ModuleNotFoundError:没有名为“mlxtend”的模块
- python - 在 Debian 10.6 上安装 Bazzar 时出现问题