J'essaie de gérer l'erreur de la requête http dans showDialog, puis de lancer une erreur mais je suis confronté à ce bug
Erreur
E / flutter (18769): # 13 TextInput._handleTextInputInvocation package: flutter / â € ¦ / services / text_input.dart: 968 E / flutter (18769): # 14 MethodChannel._handleAsMethodCall package: flutter / â € ¦ / services / platform_channel .dart: 402 E / flutter (18769): # 15 MethodChannel.setMethodCallHandler. paquet: flutter / â € ¦ / services / platform_channel.dart: 370 E / flutter (18769): # 16
_DefaultBinaryMessenger.handlePlatformMessage package: flutter /… / services / binding.dart: 200 E / flutter (18769): # 17
_invoke3. (fléchette: ui / hooks.dart: 303: 15) E / flutter (18769): # 18 _rootRun (dart: async / zone.dart: 1126: 13) E / flutter (18769): # 19 _CustomZone.run (fléchette : 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 (fléchette: ui / hooks.dart: 162: 5)
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);
});
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;
});
}
3 Réponses :
c'est parce que votre type de fonction est Future et que votre type de retour doit être Future, mais lorsque vous rencontrez une erreur, votre réponse lance une erreur et renvoie Null, il est donc préférable d'écrire votre fonction async comme ceci
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;
});
}
et votre URL est incorrecte, remplacez 'https://flutter-shop-768a7.firebaseio.com/products.jon' par 'https://flutter-shop-768a7.firebaseio.com/products.json'
cela s'est produit parce que vous n'avez pas spécifié le type de retour de la .then((response) {}) , pour résoudre ce problème, modifiez simplement
.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
à
.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();
})
Veuillez ajouter «Null» ci-dessous comme ceci. Je suis également confronté au même problème après avoir ajouté que le problème est résolu.
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')),
],
),
);