我正在尝试在showDialog中处理来自HTTP请求的错误,然后抛出一个错误,但我遇到了这个错误
错误
E / flutter(18769):#13 TextInput._handleTextInputInvocation 包:flutter /…/ services / text_input.dart:968 E / flutter(18769):#14 MethodChannel._handleAsMethodCall 包:flutter /…/ services / platform_channel.dart:402 E / flutter (18769):#15 MethodChannel.setMethodCallHandler。包:flutter /…/ services / platform_channel.dart:370 E / flutter(18769):#16
_DefaultBinaryMessenger.handlePlatformMessage软件包:flutter /…/ services / binding.dart:200 E / flutter(18769):#17
_invoke3。 (dart:ui / hooks.dart:303:15)E / flutter(18769):#18 _rootRun(dart:async / zone.dart:1126:13)E / flutter (18769):#19 _CustomZone.run(dart:async / zone.dart:1023:19) E / flutter(18769):#20 _CustomZone.runGuarded (dart:async / zone.dart:925:7)E / flutter(18769):#21 _invoke3 (dart:ui / hooks.dart:302:10)E / flutter(18769):#22
_dispatchPlatformMessage(dart:ui / hooks.dart: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);
});
答案 0 :(得分:2)
它是因为您的函数类型是Future,而您的返回类型必须是Future 但是当您遇到错误时,您的响应会引发错误并返回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;
});
}
您的网址不正确,将'https://flutter-shop-768a7.firebaseio.com/products.jon'
更改为'https://flutter-shop-768a7.firebaseio.com/products.json'
答案 1 :(得分:1)
请在下面添加“空”。添加问题解决后,我也面临着同样的问题。
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')),
],
),
);
答案 2 :(得分:0)
发生这种情况是因为您没有指定.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 here