(HttpException) => Null' is not a subtype of type '(dynamic) => dynamic'!! flutter

Viewed 737

Im trying to request JSONs and some images. The JSONs come just fine, as they are small. When im requesting images, I just get the following SocketException 95% of the time:

I/flutter ( 9249): Exception: type '(HttpException) => Null' is not a subtype of type '(dynamic) => dynamic'!!

On the server side it always appears with the status code 200. Sometimes the image does come threw and it works fine, but thats a rare event. This is the code for the request that is not working:

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;
  }

I've tried everything, I have another project that works just fine and everything is exactly the same. Any idea why the connection is beeing broken?

Edit: here's the function call : var imgBytes = await InStoreRequests.productImage(imageID);

2 Answers

Please provide the call of productImage().

Wrong function call

In case your call look something like this:

image = productImage(imageId);

Solution

Just add await to your call. Like explained in https://dart.dev/codelabs/async-await.

image = await productImage(imageId);

If you make your function asynchronous, you always have to add await to wait the function to complete. Because of async your app will not wait till your function completes and just going to proceed with the next code.

Edit: Have not seen the exception. Wrong answer.

Okey, turns out i'm dumb. It doesn't work properly on Android Studio, I tested it on a phone, and it worked just fine

Related