微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

未处理的异常:处置后使用了一个类 -颤振

如何解决未处理的异常:处置后使用了一个类 -颤振

我遇到此错误

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 View ,其中我从用户那里获取电话号码并将其发送给 viewmodel > 名为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();
  }
}

解决方法

在进行注射时,您可能使用了RegisterLazySingleton而不是Factory。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。