我試圖讓全域 http 請求類在我的所有應用程式中使用它,我在類“ webPost ”中宣告了方法以回傳 Future<http.Response> 但不接受 null 作為回傳,以防請求在任何例外時回傳 null ,在這種情況下我該怎么辦。
代碼 :
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
uj5u.com熱心網友回復:
Future<http.Response>已宣告 - 這意味著它只能回傳不可為空的。用于Future<http.Response?>允許可為空的回傳值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/434602.html
