How do I return error from a Future in dart?
//POST
Future<String> post_firebase_async({String? path , required Product product}) async { final Uri _url = path == null ? currentUrl: Uri.https(_baseUrl, '/$path'); print('Sending a POST request at $_url'); final response = await http.post(_url, body: jsonEncode(product.toJson())); if(response.statusCode == 200){ final result = jsonDecode(response.body) as Map<String,dynamic>; return result['name']; } else{ //throw HttpException(message: 'Failed with ${response.statusCode}'); return Future.error("This is the error", StackTrace.fromString("This is its trace")); } }
Here is how to call:
final result = await _firebase.post_firebase_async(product: dummyProduct). catchError((err){ print('huhu $err'); });