gpt4 book ai didi

flutter - 未处理的异常 : A class was used after being disposed. - flutter

转载 作者:行者123 更新时间:2023-12-03 22:29:27 24 4
gpt4 key购买 nike

我收到此错误

E/flutter ( 9610): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: A CustRegViewModel was used after being disposed.
E/flutter ( 9610): Once you have called dispose() on a CustRegViewModel, it can no longer be used.
我有一个 查看 命名为 CustRegView我从用户那里获取电话号码并将其发送到 查看型号 命名为 CustRegViewModel进行身份验证,根据其身份验证状态返回 bool。
class CustRegView extends StatefulWidget {
@override
_CustRegViewState createState() => _CustRegViewState();
}

class CustRegView extends StatelessWidget{
final TextEditingController _controller = TextEditingController();

@override
Widget build(BuildContext context) {
final deviceSize = MediaQuery.of(context).size;
return BaseView<CustRegViewModel>(
builder: (context, model, child) => Scaffold(

...<some code>

FlatButton (
onPressed: () async {
var registerSuccess = await model.register( _controller.text, context);
// ^^^^^ HERE, IN ABOVE LINE, I'M GETTING AN ERROR ^^^^^^

if (registerSuccess) {
Navigator.pushNamed(context, 'newScreen');
} else {
UIHelper().showErrorButtomSheet(context, model.errorMessage);
}
)
}
CustRegViewModel看起来像这样
class CustRegViewModel extends BaseViewModel {

final AuthService _authService = locator<AuthService>();
final DialogService _dialogService = locator<DialogService>();
dynamic newUserResult;
dynamic verifyResult;

Future<bool> register(String phoneNo, BuildContext context) async {
await verifyPhone;
return verifyResult ? true : false; // From here it returns true
}

Future<void> verifyPhone(phoneNo) async {

await FirebaseAuth.instance.verifyPhoneNumber(
phoneNumber: updatedPhoneNo,
timeout: Duration(seconds: 50),
verificationCompleted: (AuthCredential authCred) async {...... <some code>
verificationFailed: (AuthException authException) {...... <some code>
codeSent: (String verID, [int forceCodeResend]) async {...... <some code>
codeAutoRetrievalTimeout: (String verID) {...
).catchError((error) {...... <some code>
}
}
BaseView看起来像这样
class BaseView<T extends BaseViewModel> extends StatefulWidget {
final Widget Function(BuildContext context, T model, Widget child) builder;
final Function(T) onModelReady;
BaseView({this.builder, this.onModelReady});
@override
_BaseViewState<T> createState() => _BaseViewState<T>();
}

class _BaseViewState<T extends BaseViewModel> extends State<BaseView<T>> {
T model = locator<T>();

@override
void initState() {
if (widget.onModelReady != null) {
widget.onModelReady(model);
}
super.initState();
}

@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<T>(
create: (context) => model,
child: Consumer<T>(builder: widget.builder),
);
}
}
BaseViewModel看起来像这样
class BaseViewModel extends ChangeNotifier {
ViewState _state = ViewState.Idle;


ViewState get state => _state;

void setState(ViewState viewState) {
_state = viewState;
notifyListeners();
}
}

最佳答案

您可能在依赖注入(inject)中使用了 RegisterLazySingleton 而不是 Factory。

关于flutter - 未处理的异常 : A class was used after being disposed. - flutter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63495227/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com