我正在開發一個購物應用程式,我需要將用戶輸入資料保存到服務器。在發送資料時,我想處理例外(如果有)。但是, .catchError() 塊不起作用。
代碼:
void _saveForm() {
final isValid = _form.currentState!.validate();
if (!isValid) {
return;
}
_form.currentState!.save();
setState(() {
_isLoading = true;
});
if (ModalRoute.of(context)!.settings.arguments as String == "add") {
Provider.of<Products>(context, listen: false)
.addProduct(_editedProduct)
.catchError((error) {
return showDialog(
context: context,
builder: (ctx) => AlertDialog(
title: Text("Somethiinng went wrong"),
content: Text(error.toString()),
actions: <Widget>[
FloatingActionButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text("Okay"),
)
],
));
}).then((_) {
setState(() {
_isLoading = false;
});
});
Navigator.of(context).pop();
} else {
Provider.of<Products>(context, listen: false).updateProduct(
ModalRoute.of(context)!.settings.arguments as String, _editedProduct);
Navigator.of(context).pop();
}
}
這是我發送資料的提供程式檔案,并.json在導致例外的鏈接之后有意洗掉。
Future<void> addProduct(Product product) {
final url = Uri.parse(
'https://food-fancy-default-rtdb.firebaseio.com/products');// removed .json after products
return http
.post(url,
body: json.encode({
"title": product.title,
"price": product.price,
"description": product.description,
"imageUrl": product.imageUrl,
"isFavorite": product.isFavorite
}))
.then((response) {
final _newProduct = Product(
id: json.decode(response.body)["name"],
title: product.title,
description: product.description,
price: product.price,
imageUrl: product.imageUrl);
_items.insert(0, _newProduct);
print(json.decode(response.body));
notifyListeners();
}).catchError((error) {
throw error;
});
}
錯誤資訊:
> E/flutter (11843): [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 (11843): Consider canceling any active work during "dispose" or using the "mounted" getter to determine if the State is still active.
我努力了
Navigator.pop()
Navigator.of(context, rootNavigator: true).pop()
uj5u.com熱心網友回復:
如果我使用try catch. 阻止async
代碼:
void _saveForm() async {
final isValid = _form.currentState!.validate();
if (!isValid) {
return;
}
_form.currentState!.save();
setState(() {
_isLoading = true;
});
if (ModalRoute.of(context)!.settings.arguments as String == "add") {
try {
await Provider.of<Products>(context, listen: false)
.addProduct(_editedProduct);
} catch (error) {
await showDialog(
// returning the result of showDialogg()
context: context,
builder: (ctx) => AlertDialog(
title: const Text("Somethiinng went wrong"),
content: Text(error.toString()),
actions: <Widget>[
ElevatedButton(
child: const Text("Okay"),
onPressed: () {
Navigator.of(ctx).pop();
},
)
],
));
} finally {
setState(() {
_isLoading = false;
});
Navigator.of(context).pop();
}
} else {
Provider.of<Products>(context, listen: false).updateProduct(
ModalRoute.of(context)!.settings.arguments as String, _editedProduct);
Navigator.of(context).pop();
}
}`
uj5u.com熱心網友回復:
這ModalRoute.of(context)!.settings.arguments as String是不正確的,因為它ModalRoute.of(context)!.settings.arguments是一個映射,你不能將它轉換為一個字串,所以你需要做的是訪問它里面的鍵,例如,如果你發送一個'add'用true你需要的值呼叫的鍵像這樣訪問它:
Map<String, dynamic>? _data = ModalRoute.of(context)!.settings.arguments as Map<String, dynamic>?;
_result = _data!['add'];
結果現在應該是真的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/388071.html
上一篇:顫振中的單元測驗?
下一篇:Linux—基礎IO
