Flutter dart 调用 super of super class 方法
Flutter dart call super of super class method
这是我的架构,我有两个 statfull 小部件:ProfilePicture, RoundedProfilePicture
。 ProfilePicture
是一个有状态的小部件,它有复杂的自定义代码,很容易出错,所以我不想复制粘贴它,它利用了来自 Picture
抽象 class 的变量。 (即获取服务器数据并将它们存储在 Picture
的变量中)。我想做的是从这个小部件状态扩展,以便我可以创建一个新的小部件 RoundedProfilePicture
。这个小部件状态基本上会利用继承自 ProfilePicture
状态的复杂代码,并且会向其中添加一些额外的逻辑。所以我认为继承是这里最好的选择。这是我到目前为止尝试过的
class ProfilePictureState extends State<ProfilePicture> implements Picture{
// SOME LONG COMPLICATED 500 LINE OF CODE
}
class RoundedProfilePictureState extends ProfilePictureState {
@override
void initState() {
super.initState(); // this calls ProfilePictureState.initState() call. I want to call State<ProfilePicture>.initState()
}
}
我的问题 RoundedProfilePictureState
中的 initState()
无效,需要我拨打 super.initState()
电话。此调用进行 ProfilePictureState.initState()
调用。我想调用 State<ProfilePicture>.initState()
因为我想在我的初始状态调用中添加不同的逻辑。所以结构是:
----abstract State class
---ProfilePictureState
--RoundedProfilePictureState
如何从 RoundedProfilePictureState
调用抽象状态 class 的 initState
方法?这可能吗?
由于没有人回答,这就是我解决这个问题的方法。我认为这是不可能实现的。我所做的是将代码和所有变量移动到 mixin
。花了一两个小时才完成。我满足了我的需求。
这是我的架构,我有两个 statfull 小部件:ProfilePicture, RoundedProfilePicture
。 ProfilePicture
是一个有状态的小部件,它有复杂的自定义代码,很容易出错,所以我不想复制粘贴它,它利用了来自 Picture
抽象 class 的变量。 (即获取服务器数据并将它们存储在 Picture
的变量中)。我想做的是从这个小部件状态扩展,以便我可以创建一个新的小部件 RoundedProfilePicture
。这个小部件状态基本上会利用继承自 ProfilePicture
状态的复杂代码,并且会向其中添加一些额外的逻辑。所以我认为继承是这里最好的选择。这是我到目前为止尝试过的
class ProfilePictureState extends State<ProfilePicture> implements Picture{
// SOME LONG COMPLICATED 500 LINE OF CODE
}
class RoundedProfilePictureState extends ProfilePictureState {
@override
void initState() {
super.initState(); // this calls ProfilePictureState.initState() call. I want to call State<ProfilePicture>.initState()
}
}
我的问题 RoundedProfilePictureState
中的 initState()
无效,需要我拨打 super.initState()
电话。此调用进行 ProfilePictureState.initState()
调用。我想调用 State<ProfilePicture>.initState()
因为我想在我的初始状态调用中添加不同的逻辑。所以结构是:
----abstract State class
---ProfilePictureState
--RoundedProfilePictureState
如何从 RoundedProfilePictureState
调用抽象状态 class 的 initState
方法?这可能吗?
由于没有人回答,这就是我解决这个问题的方法。我认为这是不可能实现的。我所做的是将代码和所有变量移动到 mixin
。花了一两个小时才完成。我满足了我的需求。