[Breaking Change] Removing package:http from Flutter

1,165 views
Skip to first unread message

Jonah Williams

unread,
Mar 11, 2018, 7:21:36 PM3/11/18
to flutt...@googlegroups.com
Howdy,

https://github.com/flutter/flutter/pull/15416 will remove package:http from the dependencies of Flutter, fixing https://github.com/flutter/flutter/issues/13456.  This also removes `createHttpClient` from `flutter/services.dart`, which was previously marked deprecated.

* If you don't use package:http, you get a smaller build.
* If you do use package:http, you don't have to use the exact version that was included with Flutter. You must explicitly include package:http in your pubspec.


Jonah Williams

unread,
Mar 12, 2018, 1:08:28 AM3/12/18
to flutt...@googlegroups.com
Some more context,

Porting existing code which used createHttpClient.

createHttpClient would allow you to override the HttpClient used by NetworkAssetBundle and NetworkImage for testing purposes.  By default, flutter_test would override the client with one that always returned 400 responses (this is still the case).  If you provided your own MockClient in any tests, you can retain the same behavior using HttpOverrides from dart:io.

HttpOverrides is a class in dart:io which allows you to control per Zone what class `new HttpClient()` returns.  For example, in the code below everything inside of the callback passed to runZoned uses a custom http client.
void main() {
testWidgets('using HttpOverrides', (WidgetTester tester) async {
HttpOverrides.runZoned(() async {
// All code inside here will use the HttpClient returned below.
await tester.pumpWidget(new Image.network('http://www.foobar.com/baz.png'));
}, createHttpClient: createMockImageHttpClient);
});
}

Creating a MockHttpClient

There are a few different ways to create a mocked HttpClient.  For the example below I use package:mockito.


// Returns a mock HTTP client that responds with a blank image to all requests.
MockHttpClient createMockImageHttpClient(SecurityContext _) {
final MockHttpClient client = new MockHttpClient();
final MockHttpClientRequest request = new MockHttpClientRequest();
final MockHttpClientResponse response = new MockHttpClientResponse();
final MockHttpHeaders headers = new MockHttpHeaders();

when(client.getUrl(typed(any))).thenReturn(new Future<HttpClientRequest>.value(request));
when(request.headers).thenReturn(headers);
when(request.close()).thenReturn(new Future<HttpClientResponse>.value(response));
when(response.contentLength).thenReturn(kTransparentImage.length);
when(response.statusCode).thenReturn(HttpStatus.OK);
when(response.listen(typed(any))).thenAnswer((Invocation invocation) {
final void Function(List<int>) onData = invocation.positionalArguments[0];
final void Function() onDone = invocation.namedArguments[#onDone];
final void Function(Object, [StackTrace]) onError = invocation.namedArguments[#onError];
final bool cancelOnError = invocation.namedArguments[#cancelOnError];

return new Stream<List<int>>.fromIterable(<List<int>>[kTransparentImage])
.listen(onData, onDone: onDone, onError: onError, cancelOnError: cancelOnError);
});

return client;
}

class MockHttpClient extends Mock implements HttpClient {}

class MockHttpClientRequest extends Mock implements HttpClientRequest {}

class MockHttpClientResponse extends Mock implements HttpClientResponse {}

class MockHttpHeaders extends Mock implements HttpHeaders {}

Wrapping it up

If you are using package:http in your Flutter app you can keep using - you just need to include it in your pubspec.  If you are using createHttpClient to provide mocks for testing NetworkImage or NetworkAssetBundle, you'll need to migrate to HttpOverrides above.

If you have any questions about using HttpOverrides, or have any questions/concerns about this change then please either respond to this thread or send me an email.

Furthermore, if you want help migrating let me know and I will gladly help out.


Thanks!

Jonah

dnet...@gmail.com

unread,
Mar 28, 2018, 1:23:16 PM3/28/18
to Flutter Dev
Where do I implement this in my app?  I have lots fo places that use the createHttpClient since it was the way the docs were showing how to accomplish it.

I need to keep using it until I have time to change it in the 100+ places it is used, just not sure where to implement this code.

Ian Hickson

unread,
Mar 28, 2018, 1:26:42 PM3/28/18
to dnet...@gmail.com, Flutter Dev
You can copy the old createHttpClient code that we had into your code, just make it a file that you import from all your various files. There's actually nothing wrong with continuing to use package:http. Here's the code we used to have:

ValueGetter<http.Client> createHttpClient = () {
  return new http.Client();
};

(Apologies for removing createHttpClient, we are trying to reduce our own dependencies so that you are more free to use whatever version of these packages that you like.)

--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

--
Ian Hickson

😸
Reply all
Reply to author
Forward
0 new messages