繁体   English   中英

未处理的异常:此小部件已在 flutter 中卸载?

[英]Unhandled Exception: This widget has been unmounted, in flutter?

我正在尝试将数据提交到 API,但抛出了一个错误,提示小部件已卸载。 这是代码:

SubmitRequest() async {
    var newValue;
    if(selectedIndex == 0){
      newValue = 2;
    }else if(selectedIndex == 1){
      newValue = 3;
    }else{
      newValue = null;
    }
    var remark;
    if(selectedIndex == 0){
      remark = "Wrong";
    }else if(selectedIndex == 1){
      remark = "Invalid";
    }else{
      return null;
    }
    var url = Uri.parse(url);
    var header = {
      "API-Key": "eeee",
      "Content-Type": "application/json",
    };
    final bdy = jsonEncode({
      "action" :"dhdhd",
      "token":"df23311",
      "date": getCurrentDate().toString(),
      
    });
   
    var jsonresponse = await http.post(url, headers: header, body: bdy);
    print(jsonresponse.statusCode);
    if (jsonresponse.statusCode == 200) {
        Utils.showToastSuccess("Submitted Successfully").show(context);
        print("submit success");
    }else{
      Utils.showToastError("Submit Failed, Try Again").show(context);
    }
  }

当我尝试运行它时,吐司不会弹出并抛出错误:

E/flutter (  615): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: This widget has been unmounted, so the State no longer has a context (and should be considered defunct).
E/flutter (  615): Consider canceling any active work during "dispose" or using the "mounted" getter to determine if the State is still active.
E/flutter (  615): #0      State.context.<anonymous closure> (package:flutter/src/widgets/framework.dart:909:9)
E/flutter (  615): #1      State.context (package:flutter/src/widgets/framework.dart:915:6)
E/flutter (  615): #2      _WrongOrInvalidState.SubmitRequest (package:bmteacher/ui/History/invalid.dart:76:63)
E/flutter (  615): <asynchronous suspension>
E/flutter (  615): 

当您调用方法submitRequest()时,您是否使用了关键字await 因为它是您尝试使用已经处理的上下文的方法,所以您的应用程序可能没有等待方法submitRequest()完成。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM