【发布时间】:2021-12-06 06:32:24
【问题描述】:
例如我的课程 ProfileModel 有一堆字段
他们中的许多人没有默认值,除非当我从后端获取用户信息时它们正在初始化
对于riverpod,我需要写一些类似的东西
final profileProvider = StateNotifierProvider((ref) => ProfileState());
class ProfileState extends StateNotifier<ProfileModel> {
ProfileState() : super(null);
}
我知道我需要将 ProfileState.empty() 之类的东西传递给 super() 方法,而不是传递 null
但在这种情况下,我必须为每个 ProfileModels 字段创建默认值
这对我来说听起来很奇怪,我不想为了关心项目中每个模型的空状态或默认状态而伤脑筋
在我的示例中,用户名、年龄等没有默认值
这是纯粹的不可变类
我做错了什么或错过了什么?
或者我可以将模型声明为可空extends StateNotifier<ProfileModel?>
但我不确定这是不是一个好方法
【问题讨论】: