From 3efd2fad38c1327af1de671c3bea1b8b1ef6644f Mon Sep 17 00:00:00 2001 From: Brian Quinlan Date: Thu, 13 Jun 2024 16:28:11 -0700 Subject: [PATCH 1/2] Add a note saying that we only creating a single `Client`. --- pkgs/flutter_http_example/lib/main.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/pkgs/flutter_http_example/lib/main.dart b/pkgs/flutter_http_example/lib/main.dart index 548c3dee0a..d510c669e9 100644 --- a/pkgs/flutter_http_example/lib/main.dart +++ b/pkgs/flutter_http_example/lib/main.dart @@ -15,6 +15,7 @@ import 'http_client_factory.dart' void main() { runApp(Provider( + // Share a single `Client` throughout the application. create: (_) => http_factory.httpClient(), child: const BookSearchApp(), dispose: (_, client) => client.close())); From 1b0dadb1b6486b0fb775af2aa682f1f0b86a7cf7 Mon Sep 17 00:00:00 2001 From: Brian Quinlan Date: Thu, 13 Jun 2024 17:08:56 -0700 Subject: [PATCH 2/2] Update main.dart --- pkgs/flutter_http_example/lib/main.dart | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pkgs/flutter_http_example/lib/main.dart b/pkgs/flutter_http_example/lib/main.dart index d510c669e9..afe8363029 100644 --- a/pkgs/flutter_http_example/lib/main.dart +++ b/pkgs/flutter_http_example/lib/main.dart @@ -15,7 +15,14 @@ import 'http_client_factory.dart' void main() { runApp(Provider( - // Share a single `Client` throughout the application. + // `Provider` calls its `create` argument once when a `Client` is + // first requested (through `BuildContext.read()`) and uses that + // same instance for all future requests. + // + // Reusing the same `Client` may: + // - reduce memory usage + // - allow caching of fetched URLs + // - allow connections to be persisted create: (_) => http_factory.httpClient(), child: const BookSearchApp(), dispose: (_, client) => client.close()));