我正在尝试在showDialog中处理来自http请求的错误,然后抛出一个错误,但是我面临这个错误
误差
E/颤振(18769):#13 TextInput._handleTextInputInvocation包:颤振/…/services/text_input.dart:968E/颤振(18769):#14 MethodChannel._handleAsMethodCall包:flutter/…/services/platform_channel.dart.402:402E/颤振(18769):#15 MethodChannel.setMethodCallHandler。包装:颤振/…/services/platform_channel.dart.18769:370 E/颤振(18769):#16
_DefaultBinaryMessenger.handlePlatformMessage包:颤振/…/services/binding.dart:200E/颤振(18769):#17
_invoke3。(飞镖:ui/hooks.dart:303:15)E/ _rootRun (18769):#18 _CustomZone.runGuarded (飞镖:异步/zone.dart:1126:13)E/hooks.dart (18769):#19 _CustomZone.run (飞镖:异步/zone.dart:1023:19)E/颤振(18769):#20 _CustomZone.runGuarded(飞镖:异步/左:7)E/颤振(18769):#21_invoke3 (飞镖:ui/Hooks.DAT:302:10)E/颤振(18769):#22
_dispatchPlatformMessage (飞镖:ui/Hooks.DAT:162:5)
Future<void> addProduct(Product product) {
const url = 'https://flutter-shop-768a7.firebaseio.com/products.jon';
return http
.post(url,
body: json.encode({
'title': product.title,
'description': product.description,
'imageUrl': product.imageUrl,
'price': product.price,
'isFavorite': product.isFavorite
}))
.then((response) {
final newProduct = Product(
title: product.title,
description: product.description,
imageUrl: product.imageUrl,
price: product.price,
id: json.decode(response.body)['name']);
// _items.insert(index, element)
_items.add(newProduct);
notifyListeners();
}).catchError((error) {
throw error;
});
} Provider.of<Products>(context, listen: false)
.addProduct(_edditedProduct)
.catchError((error) {
return showDialog(
context: context,
builder: (ctx) => AlertDialog(
title: Text('An Error occurred!'),
content: Text('Someghing went wrong'),
actions: <Widget>[
FlatButton(
child: Text('ok'),
onPressed: () async => Navigator.of(context).pop())
],
),
);
}).then((_) {
print('this is then function');
setState(() {
_isLoading = false;
});
Navigator.pop(context);
});发布于 2020-01-23 08:13:53
因为您的函数类型是未来,而返回类型必须是未来,但是当您面临错误时,您的响应抛出错误并返回Null,所以最好像这样编写异步函数
addProduct(Product product) async {
const url = 'https://flutter-shop-768a7.firebaseio.com/products.json';
await http
.post(url,
body: json.encode({
'title': product.title,
'description': product.description,
'imageUrl': product.imageUrl,
'price': product.price,
'isFavorite': product.isFavorite
}))
.then((response) {
final newProduct = Product(
title: product.title,
description: product.description,
imageUrl: product.imageUrl,
price: product.price,
id: json.decode(response.body)['name']);
// _items.insert(index, element)
_items.add(newProduct);
notifyListeners();
}).catchError((error) {
throw error;
});
}而且您的url不正确,将'https://flutter-shop-768a7.firebaseio.com/products.jon'更改为'https://flutter-shop-768a7.firebaseio.com/products.json'
发布于 2020-11-13 16:58:27
请在下面加上“零”如下。在增加问题解决后,我也要面对同样的问题。
return showDialog<Null>(
context: context,
builder: (ctx) => AlertDialog(
title: Text('Error occurred!'),
content: Text('Something went wrong...'),
actions: [
FlatButton(
onPressed: () {
Navigator.of(ctx).pop();
},
child: Text('Okay')),
],
),
);发布于 2020-07-12 15:44:25
之所以发生这种情况,是因为您没有指定.then((response) {})方法的返回类型,以解决这个简单的更改
.then((response) {
final newProduct = Product(
title: product.title,
description: product.description,
imageUrl: product.imageUrl,
price: product.price,
id: json.decode(response.body)['name']);
// _items.insert(index, element)
_items.add(newProduct);
notifyListeners();
})至
.then<void>((response) {
final newProduct = Product(
title: product.title,
description: product.description,
imageUrl: product.imageUrl,
price: product.price,
id: json.decode(response.body)['name']);
// _items.insert(index, element)
_items.add(newProduct);
notifyListeners();
})de herehttps://stackoverflow.com/questions/59873712
复制相似问题