所以在我的应用程序中,我想在挂载小部件后立即发出Ajax请求,而不是在initState()
中。类似于react
ComponentWillMount()
我认为目前不可能。
这里是mounted
属性:https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/widgets/framework.dart#L974
bool get mounted => _element != null;
这里是设置_element
的时间:https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/widgets/framework.dart#L3816
_state._element = this
而且我在此代码周围看不到任何钩子会通知我们。
为什么仍不使用initState
?这可能是您想要的。这是mounted
属性上方的注释:https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/widgets/framework.dart#L967
/// After creating a [State] object and before calling [initState], the
/// framework "mounts" the [State] object by associating it with a
/// [BuildContext]. The [State] object remains mounted until the framework