![](/img/trans.png)
[英]Flutter Unhandled Exception: This widget has been unmounted, so the State no longer has a context
[英]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.