2

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

Maverick
  • 31
  • 4
  • Have u tried awaiting the image? Can ya share that code – rkdupr0n Jul 16 '20 at 12:27
  • var imgBytes = await InStoreRequests.productImage(imageID); if (imgBytes != null) print("sucess"); I already await for the image, which is why this is so weird – Maverick Jul 16 '20 at 13:19
  • try remove SocketException from the catch and just put catch(e){print("$e")} to see whats wrong! it looks like you are not managing correctly your exception – Cristian Bregant Jul 16 '20 at 13:23
  • I/flutter (10205): Exception: type '(HttpException) => Null' is not a subtype of type '(dynamic) => dynamic'!! same error shows up – Maverick Jul 16 '20 at 13:30

2 Answers2

0

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.

mfrischbutter
  • 71
  • 2
  • 8
  • var imgBytes = await productImage(imageID); already done that, still doenst work – Maverick Jul 16 '20 at 12:59
  • Try to run your app with `flutter run --verbose` for more information. – mfrischbutter Jul 16 '20 at 13:18
  • Already did, when I request the image isnt printing anything else new [ +2 ms] I/flutter (10025): Exception: type '(HttpException) => Null' is not a subtype of type '(dynamic) => dynamic'!! – Maverick Jul 16 '20 at 13:23
  • Maybe the stacktrace provide some more information to you. Use `catch(e, s) {print("$s")}` – mfrischbutter Jul 16 '20 at 13:33
  • 1
    this appears and some more that doesnt fit the comment `I/flutter (10205): Exception: #0 _invokeErrorHandler (dart:async/async_error.dart:18:23) I/flutter (10205): #1 _HandleErrorStream._handleError (dart:async/stream_pipe.dart:288:9) I/flutter (10205): #2 _ForwardingStreamSubscription._handleError (dart:async/stream_pipe.dart:170:13) I/flutter (10205): #3 _rootRunBinary (dart:async/zone.dart:1204:38) I/flutter (10205): #4 _CustomZone.runBinary (dart:async/zone.dart:1093:19) ` – Maverick Jul 16 '20 at 13:37
0

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

Maverick
  • 31
  • 4