未处理的异常:这个小部件已被卸载,在颤动中?
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);
}
}
当我尝试 运行 时,toast 不会弹出并抛出错误:
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()
完成。
我正在尝试向 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);
}
}
当我尝试 运行 时,toast 不会弹出并抛出错误:
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()
完成。