From b1be5c78d4fc319de56a0660ddaac6590e02e841 Mon Sep 17 00:00:00 2001 From: Steven Zhang Date: Mon, 2 Nov 2026 15:11:14 -0500 Subject: [PATCH 1/8] feat: Add eventSourceFactory to LDConfig --- .../flutter_client_sdk/example/lib/main.dart | 15 +++++++++++ .../lib/launchdarkly_flutter_client_sdk.dart | 9 ++++++- .../lib/src/config/ld_config.dart | 26 ++++++++++++++++++- .../test/ld_config_event_source_test.dart | 25 ++++++++++++++++++ 4 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 packages/flutter_client_sdk/test/ld_config_event_source_test.dart diff --git a/packages/flutter_client_sdk/example/lib/main.dart b/packages/flutter_client_sdk/example/lib/main.dart index 33a551a7..2e8f9aac 100644 --- a/packages/flutter_client_sdk/example/lib/main.dart +++ b/packages/flutter_client_sdk/example/lib/main.dart @@ -1,3 +1,4 @@ +import 'package:flutter/foundation.dart' show kIsWeb; import 'package:flutter/material.dart'; import 'package:launchdarkly_flutter_client_sdk/launchdarkly_flutter_client_sdk.dart'; import 'package:provider/provider.dart'; @@ -26,6 +27,20 @@ class MyApp extends StatelessWidget { // Alternatively `CredentialSource.fromEnvironment()` can be replaced with your mobile key. CredentialSource.fromEnvironment(), AutoEnvAttributes.enabled, + // Optional, and only for applications with a specific need. + // On web the default streaming client is the browser + // EventSource, which cannot send a request body or report an + // HTTP error. This opts in to an HTTP-based client on web only. + // The application tags header is not sent on this path. + eventSourceFactory: kIsWeb + ? (request) => SSEClient.http( + request.uriProvider(), request.eventTypes, + headers: request.headers, + body: request.body, + httpMethod: request.method, + logger: request.logger, + uriProvider: request.uriProvider) + : null, ), // Here we are using a default user with 'user-key'. LDContextBuilder().kind('user', 'user-key').build()), diff --git a/packages/flutter_client_sdk/lib/launchdarkly_flutter_client_sdk.dart b/packages/flutter_client_sdk/lib/launchdarkly_flutter_client_sdk.dart index 04bb27a2..1e6a0e70 100644 --- a/packages/flutter_client_sdk/lib/launchdarkly_flutter_client_sdk.dart +++ b/packages/flutter_client_sdk/lib/launchdarkly_flutter_client_sdk.dart @@ -80,7 +80,14 @@ export 'package:launchdarkly_common_client/launchdarkly_common_client.dart' StreamingInitializer, PollingSynchronizer, StreamingSynchronizer, - Fdv1FallbackConfig; + Fdv1FallbackConfig, + SSEClient, + SSEClientFactory, + SSEClientRequest, + SSECapability, + SseHttpMethod, + SseHttpError, + EventSourceLogger; export 'src/ld_client.dart' show LDClient; export 'src/config/ld_config.dart' show LDConfig, ApplicationEvents; diff --git a/packages/flutter_client_sdk/lib/src/config/ld_config.dart b/packages/flutter_client_sdk/lib/src/config/ld_config.dart index b5b7493f..ac5f34dd 100644 --- a/packages/flutter_client_sdk/lib/src/config/ld_config.dart +++ b/packages/flutter_client_sdk/lib/src/config/ld_config.dart @@ -94,6 +94,29 @@ final class LDConfig extends LDCommonConfig { /// [dataSystem] opts the SDK into the FDv2 data acquisition protocol. /// This feature is not stable, and not subject to any backwards /// compatibility guarantees or semantic versioning. It is in early access. + /// + /// [eventSourceFactory] supplies the SSE client that the SDK uses for + /// streaming. Most applications do not need it, and it is not a general + /// recommendation to use `usePost`, `useReport` or a custom client. + /// + /// On web, the default client is the browser `EventSource`. It cannot send + /// a request body or report an HTTP error. An application with a specific + /// need for these can opt in to an HTTP-based client: + /// + /// ```dart + /// LDConfig(credential, AutoEnvAttributes.enabled, + /// eventSourceFactory: (request) => SSEClient.http( + /// request.uriProvider(), request.eventTypes, + /// headers: request.headers, + /// body: request.body, + /// httpMethod: request.method, + /// logger: request.logger, + /// uriProvider: request.uriProvider)); + /// ``` + /// + /// On web, the SDK does not send the application tags header on this path. + /// The CORS allow-list of the streaming service does not include it, and + /// the browser blocks a request that sends it. LDConfig(super.sdkCredential, super.autoEnvAttributes, {super.applicationInfo, super.httpProperties, @@ -108,7 +131,8 @@ final class LDConfig extends LDCommonConfig { ApplicationEvents? applicationEvents, super.hooks, List? plugins, - super.dataSystem}) + super.dataSystem, + super.eventSourceFactory}) : applicationEvents = applicationEvents ?? ApplicationEvents(), plugins = plugins != null ? UnmodifiableListView(List.from(plugins)) : null; diff --git a/packages/flutter_client_sdk/test/ld_config_event_source_test.dart b/packages/flutter_client_sdk/test/ld_config_event_source_test.dart new file mode 100644 index 00000000..49c8f25f --- /dev/null +++ b/packages/flutter_client_sdk/test/ld_config_event_source_test.dart @@ -0,0 +1,25 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:launchdarkly_flutter_client_sdk/launchdarkly_flutter_client_sdk.dart'; + +void main() { + test('eventSourceFactory is not set by default', () { + final config = LDConfig('credential', AutoEnvAttributes.disabled); + + expect(config.eventSourceFactory, isNull); + }); + + test('eventSourceFactory is stored in the configuration', () { + SSEClient factory(SSEClientRequest request) => + SSEClient.http(request.uriProvider(), request.eventTypes, + headers: request.headers, + body: request.body, + httpMethod: request.method, + logger: request.logger, + uriProvider: request.uriProvider); + + final config = LDConfig('credential', AutoEnvAttributes.disabled, + eventSourceFactory: factory); + + expect(config.eventSourceFactory, same(factory)); + }); +} From 7bb6b91e78c50f66cb8ac4dea84852f8b2467acd Mon Sep 17 00:00:00 2001 From: Steven Zhang Date: Wed, 4 Nov 2026 15:24:37 -0500 Subject: [PATCH 2/8] fix: Export the SSE event types from the Flutter SDK --- .../lib/launchdarkly_flutter_client_sdk.dart | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/flutter_client_sdk/lib/launchdarkly_flutter_client_sdk.dart b/packages/flutter_client_sdk/lib/launchdarkly_flutter_client_sdk.dart index 1e6a0e70..4aa7a3ff 100644 --- a/packages/flutter_client_sdk/lib/launchdarkly_flutter_client_sdk.dart +++ b/packages/flutter_client_sdk/lib/launchdarkly_flutter_client_sdk.dart @@ -87,7 +87,10 @@ export 'package:launchdarkly_common_client/launchdarkly_common_client.dart' SSECapability, SseHttpMethod, SseHttpError, - EventSourceLogger; + EventSourceLogger, + Event, + MessageEvent, + OpenEvent; export 'src/ld_client.dart' show LDClient; export 'src/config/ld_config.dart' show LDConfig, ApplicationEvents; From 1c5690becbbc63d7aec604110cb4dfa40718c3b4 Mon Sep 17 00:00:00 2001 From: Steven Zhang Date: Thu, 5 Nov 2026 11:19:48 -0500 Subject: [PATCH 3/8] fix: Remove the SSE event type exports and clarify the eventSourceFactory docs - Event, MessageEvent and OpenEvent collide with package:web and dart:html in web apps. Only an SSEClient implementer needs them. - The LDConfig doc links to the SSEClientFactory contract and warns not to log the request. - The example states why it opts in to the HTTP client on web. - Add an export test and a start-to-close test for the factory. --- .../flutter_client_sdk/example/lib/main.dart | 9 +- .../lib/launchdarkly_flutter_client_sdk.dart | 5 +- .../lib/src/config/ld_config.dart | 15 +-- .../test/ld_config_event_source_test.dart | 101 ++++++++++++++++-- .../test/sse_exports_test.dart | 46 ++++++++ 5 files changed, 153 insertions(+), 23 deletions(-) create mode 100644 packages/flutter_client_sdk/test/sse_exports_test.dart diff --git a/packages/flutter_client_sdk/example/lib/main.dart b/packages/flutter_client_sdk/example/lib/main.dart index 2e8f9aac..ac5435cc 100644 --- a/packages/flutter_client_sdk/example/lib/main.dart +++ b/packages/flutter_client_sdk/example/lib/main.dart @@ -27,11 +27,10 @@ class MyApp extends StatelessWidget { // Alternatively `CredentialSource.fromEnvironment()` can be replaced with your mobile key. CredentialSource.fromEnvironment(), AutoEnvAttributes.enabled, - // Optional, and only for applications with a specific need. - // On web the default streaming client is the browser - // EventSource, which cannot send a request body or report an - // HTTP error. This opts in to an HTTP-based client on web only. - // The application tags header is not sent on this path. + // Optional. Most applications do not need it. On web, the + // default streaming client is the browser EventSource. It + // cannot send a request body or report an HTTP error. The + // example uses an HTTP-based client on web to show the option. eventSourceFactory: kIsWeb ? (request) => SSEClient.http( request.uriProvider(), request.eventTypes, diff --git a/packages/flutter_client_sdk/lib/launchdarkly_flutter_client_sdk.dart b/packages/flutter_client_sdk/lib/launchdarkly_flutter_client_sdk.dart index 4aa7a3ff..1e6a0e70 100644 --- a/packages/flutter_client_sdk/lib/launchdarkly_flutter_client_sdk.dart +++ b/packages/flutter_client_sdk/lib/launchdarkly_flutter_client_sdk.dart @@ -87,10 +87,7 @@ export 'package:launchdarkly_common_client/launchdarkly_common_client.dart' SSECapability, SseHttpMethod, SseHttpError, - EventSourceLogger, - Event, - MessageEvent, - OpenEvent; + EventSourceLogger; export 'src/ld_client.dart' show LDClient; export 'src/config/ld_config.dart' show LDConfig, ApplicationEvents; diff --git a/packages/flutter_client_sdk/lib/src/config/ld_config.dart b/packages/flutter_client_sdk/lib/src/config/ld_config.dart index ac5f34dd..f9337d44 100644 --- a/packages/flutter_client_sdk/lib/src/config/ld_config.dart +++ b/packages/flutter_client_sdk/lib/src/config/ld_config.dart @@ -96,12 +96,13 @@ final class LDConfig extends LDCommonConfig { /// compatibility guarantees or semantic versioning. It is in early access. /// /// [eventSourceFactory] supplies the SSE client that the SDK uses for - /// streaming. Most applications do not need it, and it is not a general - /// recommendation to use `usePost`, `useReport` or a custom client. + /// streaming. Most applications do not need this option. Use it only when + /// the default client cannot meet a specific need, for example + /// [DataSourceConfig.useReport] or [DataSystemConfig.usePost] on web. /// /// On web, the default client is the browser `EventSource`. It cannot send - /// a request body or report an HTTP error. An application with a specific - /// need for these can opt in to an HTTP-based client: + /// a request body or report an HTTP error. An application can opt in to an + /// HTTP-based client: /// /// ```dart /// LDConfig(credential, AutoEnvAttributes.enabled, @@ -114,9 +115,9 @@ final class LDConfig extends LDCommonConfig { /// uriProvider: request.uriProvider)); /// ``` /// - /// On web, the SDK does not send the application tags header on this path. - /// The CORS allow-list of the streaming service does not include it, and - /// the browser blocks a request that sends it. + /// See [SSEClientFactory] for how the SDK uses the client and for the + /// limits on web. [SSEClientRequest] carries the SDK credential and the + /// evaluation context. Do not log them. LDConfig(super.sdkCredential, super.autoEnvAttributes, {super.applicationInfo, super.httpProperties, diff --git a/packages/flutter_client_sdk/test/ld_config_event_source_test.dart b/packages/flutter_client_sdk/test/ld_config_event_source_test.dart index 49c8f25f..b812f0ad 100644 --- a/packages/flutter_client_sdk/test/ld_config_event_source_test.dart +++ b/packages/flutter_client_sdk/test/ld_config_event_source_test.dart @@ -1,5 +1,64 @@ +// ignore_for_file: close_sinks +// ignore_for_file: depend_on_referenced_packages + +// (close_sinks) Closing sinks ignored for Wifi connectivity mock. +// (depend_on_referenced_packages) We are using connectivity_plus_platform_interface as an indirect dependency +// for testing. + +import 'dart:async'; + +import 'package:connectivity_plus_platform_interface/connectivity_plus_platform_interface.dart'; +import 'package:flutter/widgets.dart' as widgets; import 'package:flutter_test/flutter_test.dart'; +import 'package:launchdarkly_common_client/launchdarkly_common_client.dart' + show Event, MessageEvent, OpenEvent; import 'package:launchdarkly_flutter_client_sdk/launchdarkly_flutter_client_sdk.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +final class _WifiConnected extends ConnectivityPlatform { + final StreamController> _controller = + StreamController(); + Stream>? _stream; + + @override + Future> checkConnectivity() async => + [ConnectivityResult.wifi]; + + @override + Stream> get onConnectivityChanged { + _stream ??= _controller.stream.asBroadcastStream(); + return _stream!; + } +} + +/// An SSE client that sends one empty put when the SDK listens. The SDK can +/// then start without a network connection. +final class _FakeSseClient implements SSEClient { + final SSEClientRequest request; + int closeCount = 0; + + late final StreamController _controller = + StreamController.broadcast(onListen: () { + _controller.add(const OpenEvent()); + _controller.add(const MessageEvent('put', '{}', null)); + }); + + _FakeSseClient(this.request); + + @override + Stream get stream => _controller.stream; + + @override + Future close() async { + closeCount++; + } + + @override + void restart() {} + + @override + bool hasCapability(SSECapability capability) => true; +} void main() { test('eventSourceFactory is not set by default', () { @@ -9,17 +68,45 @@ void main() { }); test('eventSourceFactory is stored in the configuration', () { - SSEClient factory(SSEClientRequest request) => - SSEClient.http(request.uriProvider(), request.eventTypes, - headers: request.headers, - body: request.body, - httpMethod: request.method, - logger: request.logger, - uriProvider: request.uriProvider); + SSEClient factory(SSEClientRequest request) => throw UnimplementedError(); final config = LDConfig('credential', AutoEnvAttributes.disabled, eventSourceFactory: factory); expect(config.eventSourceFactory, same(factory)); }); + + group('with a started client', () { + setUpAll(() { + ConnectivityPlatform.instance = _WifiConnected(); + widgets.WidgetsFlutterBinding.ensureInitialized(); + SharedPreferences.setMockInitialValues({}); + }); + + test('eventSourceFactory creates the streaming client', () async { + final created = <_FakeSseClient>[]; + final config = LDConfig('mobile-key', AutoEnvAttributes.disabled, + events: EventsConfig(disabled: true), + applicationEvents: ApplicationEvents( + backgrounding: false, networkAvailability: false), + persistence: PersistenceConfig(maxCachedContexts: 0), + eventSourceFactory: (request) { + final client = _FakeSseClient(request); + created.add(client); + return client; + }); + final client = + LDClient(config, LDContextBuilder().kind('user', 'user-key').build()); + + final started = await client + .start() + .timeout(const Duration(seconds: 5), onTimeout: () => false); + await client.close(); + + expect(started, isTrue); + expect(created, hasLength(1)); + expect(created.single.request.method, SseHttpMethod.get); + expect(created.single.closeCount, 1); + }); + }); } diff --git a/packages/flutter_client_sdk/test/sse_exports_test.dart b/packages/flutter_client_sdk/test/sse_exports_test.dart new file mode 100644 index 00000000..0e26b1ae --- /dev/null +++ b/packages/flutter_client_sdk/test/sse_exports_test.dart @@ -0,0 +1,46 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:launchdarkly_flutter_client_sdk/launchdarkly_flutter_client_sdk.dart'; + +final class _Logger implements EventSourceLogger { + final List messages = []; + + @override + void debug(String message) => messages.add(message); + + @override + void info(String message) => messages.add(message); + + @override + void warn(String message) => messages.add(message); + + @override + void error(String message) => messages.add(message); +} + +/// Names every SSE type that the SDK exports for a factory author. The test +/// does not compile when the SDK stops exporting one of them. +void main() { + test('the SSE types for a factory author are exported', () { + final logger = _Logger(); + SSEClient build(SSEClientRequest request) => + SSEClient.http(request.uriProvider(), request.eventTypes, + headers: request.headers, + body: request.body, + httpMethod: request.method, + logger: logger, + uriProvider: request.uriProvider); + final SSEClientFactory factory = build; + const capabilities = { + SSECapability.customMethod, + SSECapability.httpErrors, + SSECapability.requestHeaders, + }; + const error = SseHttpError(401, {}, recoverable: false); + const method = SseHttpMethod.report; + + expect(factory, same(build)); + expect(capabilities, hasLength(3)); + expect(error.statusCode, 401); + expect(method, isNot(SseHttpMethod.get)); + }); +} From f36933eddec8d49269386d007ce7e8046abb8370 Mon Sep 17 00:00:00 2001 From: Steven Zhang Date: Thu, 5 Nov 2026 12:02:16 -0500 Subject: [PATCH 4/8] refactor: Replace eventSourceFactory with useHttpStreamingOnWeb --- .../flutter_client_sdk/example/lib/main.dart | 14 +-- .../lib/launchdarkly_flutter_client_sdk.dart | 9 +- .../lib/src/config/ld_config.dart | 31 ++--- .../test/ld_config_event_source_test.dart | 112 ------------------ .../test/ld_config_streaming_test.dart | 17 +++ .../test/sse_exports_test.dart | 46 ------- 6 files changed, 28 insertions(+), 201 deletions(-) delete mode 100644 packages/flutter_client_sdk/test/ld_config_event_source_test.dart create mode 100644 packages/flutter_client_sdk/test/ld_config_streaming_test.dart delete mode 100644 packages/flutter_client_sdk/test/sse_exports_test.dart diff --git a/packages/flutter_client_sdk/example/lib/main.dart b/packages/flutter_client_sdk/example/lib/main.dart index ac5435cc..d3b0f468 100644 --- a/packages/flutter_client_sdk/example/lib/main.dart +++ b/packages/flutter_client_sdk/example/lib/main.dart @@ -1,4 +1,3 @@ -import 'package:flutter/foundation.dart' show kIsWeb; import 'package:flutter/material.dart'; import 'package:launchdarkly_flutter_client_sdk/launchdarkly_flutter_client_sdk.dart'; import 'package:provider/provider.dart'; @@ -30,16 +29,9 @@ class MyApp extends StatelessWidget { // Optional. Most applications do not need it. On web, the // default streaming client is the browser EventSource. It // cannot send a request body or report an HTTP error. The - // example uses an HTTP-based client on web to show the option. - eventSourceFactory: kIsWeb - ? (request) => SSEClient.http( - request.uriProvider(), request.eventTypes, - headers: request.headers, - body: request.body, - httpMethod: request.method, - logger: request.logger, - uriProvider: request.uriProvider) - : null, + // example streams with the HTTP-based client on web to show + // the option. It has no effect on other platforms. + useHttpStreamingOnWeb: true, ), // Here we are using a default user with 'user-key'. LDContextBuilder().kind('user', 'user-key').build()), diff --git a/packages/flutter_client_sdk/lib/launchdarkly_flutter_client_sdk.dart b/packages/flutter_client_sdk/lib/launchdarkly_flutter_client_sdk.dart index 1e6a0e70..04bb27a2 100644 --- a/packages/flutter_client_sdk/lib/launchdarkly_flutter_client_sdk.dart +++ b/packages/flutter_client_sdk/lib/launchdarkly_flutter_client_sdk.dart @@ -80,14 +80,7 @@ export 'package:launchdarkly_common_client/launchdarkly_common_client.dart' StreamingInitializer, PollingSynchronizer, StreamingSynchronizer, - Fdv1FallbackConfig, - SSEClient, - SSEClientFactory, - SSEClientRequest, - SSECapability, - SseHttpMethod, - SseHttpError, - EventSourceLogger; + Fdv1FallbackConfig; export 'src/ld_client.dart' show LDClient; export 'src/config/ld_config.dart' show LDConfig, ApplicationEvents; diff --git a/packages/flutter_client_sdk/lib/src/config/ld_config.dart b/packages/flutter_client_sdk/lib/src/config/ld_config.dart index f9337d44..d6a4a0e8 100644 --- a/packages/flutter_client_sdk/lib/src/config/ld_config.dart +++ b/packages/flutter_client_sdk/lib/src/config/ld_config.dart @@ -95,29 +95,12 @@ final class LDConfig extends LDCommonConfig { /// This feature is not stable, and not subject to any backwards /// compatibility guarantees or semantic versioning. It is in early access. /// - /// [eventSourceFactory] supplies the SSE client that the SDK uses for - /// streaming. Most applications do not need this option. Use it only when - /// the default client cannot meet a specific need, for example - /// [DataSourceConfig.useReport] or [DataSystemConfig.usePost] on web. - /// - /// On web, the default client is the browser `EventSource`. It cannot send - /// a request body or report an HTTP error. An application can opt in to an - /// HTTP-based client: - /// - /// ```dart - /// LDConfig(credential, AutoEnvAttributes.enabled, - /// eventSourceFactory: (request) => SSEClient.http( - /// request.uriProvider(), request.eventTypes, - /// headers: request.headers, - /// body: request.body, - /// httpMethod: request.method, - /// logger: request.logger, - /// uriProvider: request.uriProvider)); - /// ``` - /// - /// See [SSEClientFactory] for how the SDK uses the client and for the - /// limits on web. [SSEClientRequest] carries the SDK credential and the - /// evaluation context. Do not log them. + /// [useHttpStreamingOnWeb] streams with the HTTP-based SSE client on web. + /// The default client on web is the browser `EventSource`. It cannot send + /// a request body or report an HTTP error, so [DataSourceConfig.useReport] + /// falls back to GET and [DataSystemConfig.usePost] falls back to polling. + /// With this option, both methods work on web. The option has no effect on + /// other platforms. Most applications do not need it. LDConfig(super.sdkCredential, super.autoEnvAttributes, {super.applicationInfo, super.httpProperties, @@ -133,7 +116,7 @@ final class LDConfig extends LDCommonConfig { super.hooks, List? plugins, super.dataSystem, - super.eventSourceFactory}) + super.useHttpStreamingOnWeb}) : applicationEvents = applicationEvents ?? ApplicationEvents(), plugins = plugins != null ? UnmodifiableListView(List.from(plugins)) : null; diff --git a/packages/flutter_client_sdk/test/ld_config_event_source_test.dart b/packages/flutter_client_sdk/test/ld_config_event_source_test.dart deleted file mode 100644 index b812f0ad..00000000 --- a/packages/flutter_client_sdk/test/ld_config_event_source_test.dart +++ /dev/null @@ -1,112 +0,0 @@ -// ignore_for_file: close_sinks -// ignore_for_file: depend_on_referenced_packages - -// (close_sinks) Closing sinks ignored for Wifi connectivity mock. -// (depend_on_referenced_packages) We are using connectivity_plus_platform_interface as an indirect dependency -// for testing. - -import 'dart:async'; - -import 'package:connectivity_plus_platform_interface/connectivity_plus_platform_interface.dart'; -import 'package:flutter/widgets.dart' as widgets; -import 'package:flutter_test/flutter_test.dart'; -import 'package:launchdarkly_common_client/launchdarkly_common_client.dart' - show Event, MessageEvent, OpenEvent; -import 'package:launchdarkly_flutter_client_sdk/launchdarkly_flutter_client_sdk.dart'; -import 'package:shared_preferences/shared_preferences.dart'; - -final class _WifiConnected extends ConnectivityPlatform { - final StreamController> _controller = - StreamController(); - Stream>? _stream; - - @override - Future> checkConnectivity() async => - [ConnectivityResult.wifi]; - - @override - Stream> get onConnectivityChanged { - _stream ??= _controller.stream.asBroadcastStream(); - return _stream!; - } -} - -/// An SSE client that sends one empty put when the SDK listens. The SDK can -/// then start without a network connection. -final class _FakeSseClient implements SSEClient { - final SSEClientRequest request; - int closeCount = 0; - - late final StreamController _controller = - StreamController.broadcast(onListen: () { - _controller.add(const OpenEvent()); - _controller.add(const MessageEvent('put', '{}', null)); - }); - - _FakeSseClient(this.request); - - @override - Stream get stream => _controller.stream; - - @override - Future close() async { - closeCount++; - } - - @override - void restart() {} - - @override - bool hasCapability(SSECapability capability) => true; -} - -void main() { - test('eventSourceFactory is not set by default', () { - final config = LDConfig('credential', AutoEnvAttributes.disabled); - - expect(config.eventSourceFactory, isNull); - }); - - test('eventSourceFactory is stored in the configuration', () { - SSEClient factory(SSEClientRequest request) => throw UnimplementedError(); - - final config = LDConfig('credential', AutoEnvAttributes.disabled, - eventSourceFactory: factory); - - expect(config.eventSourceFactory, same(factory)); - }); - - group('with a started client', () { - setUpAll(() { - ConnectivityPlatform.instance = _WifiConnected(); - widgets.WidgetsFlutterBinding.ensureInitialized(); - SharedPreferences.setMockInitialValues({}); - }); - - test('eventSourceFactory creates the streaming client', () async { - final created = <_FakeSseClient>[]; - final config = LDConfig('mobile-key', AutoEnvAttributes.disabled, - events: EventsConfig(disabled: true), - applicationEvents: ApplicationEvents( - backgrounding: false, networkAvailability: false), - persistence: PersistenceConfig(maxCachedContexts: 0), - eventSourceFactory: (request) { - final client = _FakeSseClient(request); - created.add(client); - return client; - }); - final client = - LDClient(config, LDContextBuilder().kind('user', 'user-key').build()); - - final started = await client - .start() - .timeout(const Duration(seconds: 5), onTimeout: () => false); - await client.close(); - - expect(started, isTrue); - expect(created, hasLength(1)); - expect(created.single.request.method, SseHttpMethod.get); - expect(created.single.closeCount, 1); - }); - }); -} diff --git a/packages/flutter_client_sdk/test/ld_config_streaming_test.dart b/packages/flutter_client_sdk/test/ld_config_streaming_test.dart new file mode 100644 index 00000000..b140371d --- /dev/null +++ b/packages/flutter_client_sdk/test/ld_config_streaming_test.dart @@ -0,0 +1,17 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:launchdarkly_flutter_client_sdk/launchdarkly_flutter_client_sdk.dart'; + +void main() { + test('useHttpStreamingOnWeb is off by default', () { + final config = LDConfig('credential', AutoEnvAttributes.disabled); + + expect(config.useHttpStreamingOnWeb, isFalse); + }); + + test('useHttpStreamingOnWeb is stored in the configuration', () { + final config = LDConfig('credential', AutoEnvAttributes.disabled, + useHttpStreamingOnWeb: true); + + expect(config.useHttpStreamingOnWeb, isTrue); + }); +} diff --git a/packages/flutter_client_sdk/test/sse_exports_test.dart b/packages/flutter_client_sdk/test/sse_exports_test.dart deleted file mode 100644 index 0e26b1ae..00000000 --- a/packages/flutter_client_sdk/test/sse_exports_test.dart +++ /dev/null @@ -1,46 +0,0 @@ -import 'package:flutter_test/flutter_test.dart'; -import 'package:launchdarkly_flutter_client_sdk/launchdarkly_flutter_client_sdk.dart'; - -final class _Logger implements EventSourceLogger { - final List messages = []; - - @override - void debug(String message) => messages.add(message); - - @override - void info(String message) => messages.add(message); - - @override - void warn(String message) => messages.add(message); - - @override - void error(String message) => messages.add(message); -} - -/// Names every SSE type that the SDK exports for a factory author. The test -/// does not compile when the SDK stops exporting one of them. -void main() { - test('the SSE types for a factory author are exported', () { - final logger = _Logger(); - SSEClient build(SSEClientRequest request) => - SSEClient.http(request.uriProvider(), request.eventTypes, - headers: request.headers, - body: request.body, - httpMethod: request.method, - logger: logger, - uriProvider: request.uriProvider); - final SSEClientFactory factory = build; - const capabilities = { - SSECapability.customMethod, - SSECapability.httpErrors, - SSECapability.requestHeaders, - }; - const error = SseHttpError(401, {}, recoverable: false); - const method = SseHttpMethod.report; - - expect(factory, same(build)); - expect(capabilities, hasLength(3)); - expect(error.statusCode, 401); - expect(method, isNot(SseHttpMethod.get)); - }); -} From 1d3d489be0dfe2b1808ce1385cdded0fbe678bff Mon Sep 17 00:00:00 2001 From: Steven Zhang Date: Thu, 5 Nov 2026 17:53:14 -0500 Subject: [PATCH 5/8] chore: Keep the example app to basic usage --- packages/flutter_client_sdk/example/lib/main.dart | 6 ------ 1 file changed, 6 deletions(-) diff --git a/packages/flutter_client_sdk/example/lib/main.dart b/packages/flutter_client_sdk/example/lib/main.dart index d3b0f468..33a551a7 100644 --- a/packages/flutter_client_sdk/example/lib/main.dart +++ b/packages/flutter_client_sdk/example/lib/main.dart @@ -26,12 +26,6 @@ class MyApp extends StatelessWidget { // Alternatively `CredentialSource.fromEnvironment()` can be replaced with your mobile key. CredentialSource.fromEnvironment(), AutoEnvAttributes.enabled, - // Optional. Most applications do not need it. On web, the - // default streaming client is the browser EventSource. It - // cannot send a request body or report an HTTP error. The - // example streams with the HTTP-based client on web to show - // the option. It has no effect on other platforms. - useHttpStreamingOnWeb: true, ), // Here we are using a default user with 'user-key'. LDContextBuilder().kind('user', 'user-key').build()), From 8526fda390f02c2e6aa03a3175c558c8b5177cb8 Mon Sep 17 00:00:00 2001 From: Steven Zhang Date: Mon, 9 Nov 2026 09:49:58 -0500 Subject: [PATCH 6/8] refactor: Replace useHttpStreamingOnWeb on LDConfig with an eventSource option that supplies the SSE client --- .../lib/launchdarkly_flutter_client_sdk.dart | 4 +++- .../lib/src/config/ld_config.dart | 16 +++++++++------- .../test/ld_config_streaming_test.dart | 10 +++++----- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/packages/flutter_client_sdk/lib/launchdarkly_flutter_client_sdk.dart b/packages/flutter_client_sdk/lib/launchdarkly_flutter_client_sdk.dart index 04bb27a2..67b443c1 100644 --- a/packages/flutter_client_sdk/lib/launchdarkly_flutter_client_sdk.dart +++ b/packages/flutter_client_sdk/lib/launchdarkly_flutter_client_sdk.dart @@ -80,7 +80,9 @@ export 'package:launchdarkly_common_client/launchdarkly_common_client.dart' StreamingInitializer, PollingSynchronizer, StreamingSynchronizer, - Fdv1FallbackConfig; + Fdv1FallbackConfig, + SSEClient, + SseClientBuilder; export 'src/ld_client.dart' show LDClient; export 'src/config/ld_config.dart' show LDConfig, ApplicationEvents; diff --git a/packages/flutter_client_sdk/lib/src/config/ld_config.dart b/packages/flutter_client_sdk/lib/src/config/ld_config.dart index d6a4a0e8..21101dee 100644 --- a/packages/flutter_client_sdk/lib/src/config/ld_config.dart +++ b/packages/flutter_client_sdk/lib/src/config/ld_config.dart @@ -95,12 +95,14 @@ final class LDConfig extends LDCommonConfig { /// This feature is not stable, and not subject to any backwards /// compatibility guarantees or semantic versioning. It is in early access. /// - /// [useHttpStreamingOnWeb] streams with the HTTP-based SSE client on web. - /// The default client on web is the browser `EventSource`. It cannot send - /// a request body or report an HTTP error, so [DataSourceConfig.useReport] - /// falls back to GET and [DataSystemConfig.usePost] falls back to polling. - /// With this option, both methods work on web. The option has no effect on - /// other platforms. Most applications do not need it. + /// [eventSource] builds the SSE client that the SDK uses for streaming. + /// When it is null, the SDK uses the default client for the platform. On + /// web, that is the browser `EventSource`. It cannot send a request body + /// or report an HTTP error, so [DataSourceConfig.useReport] falls back to + /// GET and [DataSystemConfig.usePost] falls back to polling. Pass + /// `SSEClient.http` to stream with `fetch()` instead. Both methods then + /// work on web. Most applications do not need this option. A web build + /// that leaves it null does not include the HTTP-based client. LDConfig(super.sdkCredential, super.autoEnvAttributes, {super.applicationInfo, super.httpProperties, @@ -116,7 +118,7 @@ final class LDConfig extends LDCommonConfig { super.hooks, List? plugins, super.dataSystem, - super.useHttpStreamingOnWeb}) + super.eventSource}) : applicationEvents = applicationEvents ?? ApplicationEvents(), plugins = plugins != null ? UnmodifiableListView(List.from(plugins)) : null; diff --git a/packages/flutter_client_sdk/test/ld_config_streaming_test.dart b/packages/flutter_client_sdk/test/ld_config_streaming_test.dart index b140371d..dabbacca 100644 --- a/packages/flutter_client_sdk/test/ld_config_streaming_test.dart +++ b/packages/flutter_client_sdk/test/ld_config_streaming_test.dart @@ -2,16 +2,16 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:launchdarkly_flutter_client_sdk/launchdarkly_flutter_client_sdk.dart'; void main() { - test('useHttpStreamingOnWeb is off by default', () { + test('eventSource is null by default', () { final config = LDConfig('credential', AutoEnvAttributes.disabled); - expect(config.useHttpStreamingOnWeb, isFalse); + expect(config.eventSource, isNull); }); - test('useHttpStreamingOnWeb is stored in the configuration', () { + test('eventSource is stored in the configuration', () { final config = LDConfig('credential', AutoEnvAttributes.disabled, - useHttpStreamingOnWeb: true); + eventSource: SSEClient.http); - expect(config.useHttpStreamingOnWeb, isTrue); + expect(config.eventSource, same(SSEClient.http)); }); } From 80ad41b29f3903056724437f68b6bda495117696 Mon Sep 17 00:00:00 2001 From: Steven Zhang Date: Mon, 9 Nov 2026 10:53:12 -0500 Subject: [PATCH 7/8] docs: State that eventSource on LDConfig must supply a client that can send a request body --- packages/flutter_client_sdk/lib/src/config/ld_config.dart | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/flutter_client_sdk/lib/src/config/ld_config.dart b/packages/flutter_client_sdk/lib/src/config/ld_config.dart index 21101dee..f3925cf5 100644 --- a/packages/flutter_client_sdk/lib/src/config/ld_config.dart +++ b/packages/flutter_client_sdk/lib/src/config/ld_config.dart @@ -101,8 +101,9 @@ final class LDConfig extends LDCommonConfig { /// or report an HTTP error, so [DataSourceConfig.useReport] falls back to /// GET and [DataSystemConfig.usePost] falls back to polling. Pass /// `SSEClient.http` to stream with `fetch()` instead. Both methods then - /// work on web. Most applications do not need this option. A web build - /// that leaves it null does not include the HTTP-based client. + /// work on web. Pass only a client that can send a request body. Most + /// applications do not need this option. A web build that leaves it null + /// does not include the HTTP-based client. LDConfig(super.sdkCredential, super.autoEnvAttributes, {super.applicationInfo, super.httpProperties, From 6be783aa0f2144c9d23ece60823e661906b00be3 Mon Sep 17 00:00:00 2001 From: Steven Zhang Date: Thu, 12 Nov 2026 14:10:09 -0500 Subject: [PATCH 8/8] chore: Update launchdarkly_common_client to version 1.16.0 --- packages/flutter_client_sdk/pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/flutter_client_sdk/pubspec.yaml b/packages/flutter_client_sdk/pubspec.yaml index a6e3918b..c3a480b7 100644 --- a/packages/flutter_client_sdk/pubspec.yaml +++ b/packages/flutter_client_sdk/pubspec.yaml @@ -13,7 +13,7 @@ dependencies: sdk: flutter package_info_plus: ">=4.2.0 <11.0.0" device_info_plus: ">=9.1.1 <14.0.0" - launchdarkly_common_client: 1.15.1 + launchdarkly_common_client: 1.16.0 shared_preferences: ^2.2.2 connectivity_plus: ">=5.0.2 <8.0.0" web: ^1.1.1