I have trying to made global http request class to use it in all my app, I declared method inside class "webPost" to return Future<http.Response> but didn't accept null as return, in case request returns null on any exeption , what should I do in this case .
code :
class TWebRequest{
late int errorCode = -1 ;
late String errorMessage = "";
late bool successed = false;
Future<http.Response> webPost(String url , Object? body , Map<String,String> headers ) async
{
final http.Response response ;
try {
response =
await http.post(Uri.parse(url),
headers: headers ,body: body);
return response;
} on SocketException {
errorCode = 10000 ;
errorMessage = "SocketException";
} on HttpException {
errorCode = 10001 ;
errorMessage = "HttpException";
} on FormatException {
errorCode = 10002 ;
errorMessage = "FormatException";
return null ; //not accept
}
finally {
}
} //end void
} //end class
CodePudding user response:
Future<http.Response>
is declared - this means that it can only return non-nullable. Use Future<http.Response?>
to allow nullable return value.