1

That works fine on Android/Desktop:

Future<List<City>> fetchCities() async {


  final response =
  await http.get(globals.url + '/city',
      headers: {HttpHeaders.acceptHeader: globals.apiVersion});

  if (response.statusCode == 200) {
    // If the call to the server was successful, parse the JSON
    return compute(parseCities, response.body);
  } else {
    // If that call was not successful, throw an error.
    print('Failed to load cities');
    throw TextException(Strings.msg_session_expired);
  }
}

But when launching as web application, I'm getting an exception:

errors.dart:145 Uncaught (in promise) Error: Unsupported operation: Platform._version
    at Object.throw_ [as throw] (errors.dart:194)
    at Function._version (io_patch.dart:284)
    at Function.get version [as version] (platform_impl.dart:121)
    at get _version (platform.dart:74)
    at Function.desc.get [as _version] (utils.dart:75)
    at Function.get version [as version] (platform.dart:231)
    at Object._getHttpVersion (http_impl.dart:3234)
    at new _http._HttpClient.new (http_impl.dart:2071)
    at Function.new (http.dart:1473)
    at new io_client.IOClient.new (io_client.dart:23)
    at Function.new (client.dart:30)
    at _withClient (http.dart:165)
    at _withClient.next (<anonymous>)
    at runBody (async_patch.dart:84)
    at Object._async [as async] (async_patch.dart:123)
    at Object._withClient (http.dart:164)
    at Object.get (http.dart:47)
    at fetchCities (mcity.dart:113)
    at fetchCities.next (<anonymous>)
    at runBody (async_patch.dart:84)
    at Object._async [as async] (async_patch.dart:123)

Is there any workarond to make http.get requests working on flutter_web?

user1209216
  • 7,404
  • 12
  • 60
  • 123

1 Answers1

2

The usage of HttpHeaders.acceptHeader could be the issue. Its part of dart:io library, which is not enabled for flutter web. You should try instead to set the headers differently. For e.g. based on this constant value here something as follows should work.

final response =
  await http.get(globals.url + '/city',
      headers: {"accept": globals.apiVersion});

In another post I have used the http package which shows that this package can be used for flutter web.

Abhilash Chandran
  • 6,803
  • 3
  • 32
  • 50
  • That does not help. As I can see, http uses dart.io internally and it still throws an error. It seems, I cannot use any custom headers, otherwise it will throw error. – user1209216 Nov 18 '19 at 10:28
  • I removed any headers from my call and I'm still getting the same error. I don't understand how did you make it working. – user1209216 Nov 18 '19 at 10:36
  • Can you include your import statements. You are right `http` package uses `dart:io` but it does so by conditionally importing them. Meaning it will alternate between `dart:html` and dart:io` depending on the context it is running. In your exception snapshot I see `at new io_client.IOClient.new (io_client.dart:23)` which is from `http` package. It is somehow picking the `io_client` instead of `browser_client`. Could you simply write how you deploy your code or run it.. – Abhilash Chandran Nov 18 '19 at 11:05
  • I even removed dart.io from imports. Still I'm getting error. Flutter version from master channel. Which version have you tested? – user1209216 Nov 18 '19 at 12:02
  • I suppose this is the latest because i just upgraded to day morning. :) Doctor summary (to see all details, run flutter doctor -v): [√] Flutter (Channel master, v1.12.3-pre.49, on Microsoft Windows [Version 10.0.17134.1069], locale de-DE) [√] Android toolchain - develop for Android devices (Android SDK version 28.0.3) [√] Chrome - develop for the web [√] Android Studio (version 3.5) [√] VS Code (version 1.40.1) [√] Connected device (2 available) • No issues found! – Abhilash Chandran Nov 18 '19 at 12:27
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/202569/discussion-between-abhilash-chandran-and-user1209216). – Abhilash Chandran Nov 18 '19 at 12:36