我在addproduct对象中添加了catch错误,也在屏幕中添加了它,这样我就可以看到错误消息。因此,我在解析时故意从firebase服务器中删除了".json",因此它没有保存,并在此处给出了errorenter图像描述这就是错误:
但不知何故,错误消息没有在应用程序上显示给用户。
产品代码(addproduct对象(:
class Products with ChangeNotifier {
List<Product> _items = [
Product(
id: 'p1',
title: 'Red Shirt',
description: 'A red shirt - it is pretty red!',
price: 29.99,
imageUrl:
'https://cdn.pixabay.com/photo/2016/10/02/22/17/red-t-shirt-1710578_1280.jpg',
),
Product(
id: 'p2',
title: 'Trousers',
description: 'A nice pair of trousers.',
price: 59.99,
imageUrl:
'https://upload.wikimedia.org/wikipedia/commons/thumb/e/e8/Trousers%2C_dress_%28AM_1960.022-8%29.jpg/512px-Trousers%2C_dress_%28AM_1960.022-8%29.jpg',
),
Product(
id: 'p3',
title: 'Yellow Scarf',
description: 'Warm and cozy - exactly what you need for the winter.',
price: 19.99,
imageUrl:
'https://live.staticflickr.com/4043/4438260868_cc79b3369d_z.jpg',
),
Product(
id: 'p4',
title: 'A Pan',
description: 'Prepare any meal you want.',
price: 49.99,
imageUrl:
'https://upload.wikimedia.org/wikipedia/commons/thumb/1/14/Cast-Iron-Pan.jpg/1024px-Cast-Iron-Pan.jpg',
),
];
List<Product> get items {
return [..._items];
}
Product findById(String id) {
return _items.firstWhere((element) => element.id == id);
}
Future<void> addProduct(Product product) {
var url = Uri.parse(
'Server link without .json');
return http
.post(
url,
body: json.encode({
'title': product.title,
'description': product.description,
'imageUrl': product.imageUrl,
'price': product.price,
'Favorite': product.isFavourite
}),
)
.then((response) {
print(jsonDecode(response.body));
final newProduct = Product(
description: product.description,
id: jsonDecode(response.body)['name'],
imageUrl: product.imageUrl,
price: product.price,
title: product.title);
_items.add(newProduct);
notifyListeners();
}).catchError((error) {
print(error);
throw error;
});
}
void updateProduct(String id, Product newProduct) {
final prodIndex = _items.indexWhere((prod) => prod.id == id);
if (prodIndex >= 0) {
_items[prodIndex] =
newProduct;
notifyListeners();
} else {
print('...');
}
}
编辑产品屏幕保存表单对象代码:
void _saveForm() {
final isValid =
_form.currentState!.validate();
if (!isValid) {
return;
}
_form.currentState!.save();
setState(() {
_isLoading = true;
});
if (_editedProduct.id != '') {
Provider.of<Products>(context, listen: false)
.updateProduct(_editedProduct.id, _editedProduct);
setState(() {
_isLoading = false;
});
Navigator.of(context).pop();
} else {
Provider.of<Products>(context, listen: false)
.addProduct(_editedProduct)
.catchError((error) {
return showDialog(
context: context,
builder: (ctx) => AlertDialog(
title: Text('An error Occured'),
content: Text('Something went wrong'),
actions: [
ElevatedButton(
onPressed: () {
Navigator.of(ctx).pop();
},
child: Text('Okay'))
],
));
}).then((_) {
setState(() {
_isLoading = false;
});
Navigator.of(context).pop();
});
}
}
提前感谢!!!
我认为如果您不返回showDialog ,问题就会得到解决
.catchError((error) {
showDialog(
context: context,
builder: (ctx) => AlertDialog(
title: Text('An error Occured'),
content: Text('Something went wrong'),
actions: [
ElevatedButton(
onPressed: () {
Navigator.of(ctx).pop();
},
child: Text('Okay'))
],
));
})