我正在尝试请求JSON和一些图像。 JSON非常好,因为它们很小。当我请求图像时,我只是在95%的时间内得到以下SocketException:
I/flutter ( 9249): Exception: type '(HttpException) => Null' is not a subtype of type '(dynamic) => dynamic'!!
在服务器端,它始终以状态代码200出现。有时镜像确实被抛出并且可以正常工作,但这很罕见。这是无效的请求的代码:
static Future<Uint8List> productImage(int imageID) async {
String basicAuth =
'Basic ' + base64Encode(utf8.encode("${Request.token}:"));
http.Response response;
http.Client client = http.Client();
try {
response = await client.get(
Uri.encodeFull("$serverURL/inStoreProduct/getProductImage/$imageID"),
headers: <String, String>{'Authorization': basicAuth},
);
} catch (SocketException) {
print(
"Exception: ${SocketException.toString()}!! Route: $serverURL/inStoreProduct/getProductImage/$imageID");
return null;
}
print(
"Route: $serverURL/inStoreProduct/getProductImage/$imageID -> ${response.statusCode}");
if (response.statusCode != 200) return null;
return response.bodyBytes;
}
我已经尝试了一切,我还有另一个可以正常工作的项目,并且一切都完全一样。知道为什么连接断开了吗?编辑:
这是函数调用:
var imgBytes = await InStoreRequests.productImage(imageID);
最佳答案
请提供对productImage()
的调用。
错误的函数调用
如果您的电话看起来像这样:
image = productImage(imageId);
解只需将
await
添加到您的通话中即可。就像https://dart.dev/codelabs/async-await中解释的那样。image = await productImage(imageId);
如果使函数异步,则始终必须添加await
以等待函数完成。由于异步,您的应用程序将等到您的功能完成后才继续执行下一个代码。编辑:
没有看到异常。错误的答案。
关于flutter - (HttpException)=> Null'不是 '(dynamic) => dynamic'类型的子类型! flutter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62934646/