diff --git a/lib/unleash_proxy_client_flutter.dart b/lib/unleash_proxy_client_flutter.dart index 566f12f..d649868 100644 --- a/lib/unleash_proxy_client_flutter.dart +++ b/lib/unleash_proxy_client_flutter.dart @@ -107,6 +107,9 @@ class UnleashClient extends EventEmitter { /// The flag used commonly for "disabled" feature toggles that are not visible to frontend SDKs. bool impressionDataAll; + /// Internal indicator if the client has been started + var started = false; + UnleashClient( {required this.url, required this.clientKey, @@ -240,7 +243,10 @@ class UnleashClient extends EventEmitter { } Future updateContext(UnleashContext unleashContext) async { - if (clientState == ClientState.ready) { + if (started == false) { + await _waitForEvent('initialized'); + _updateContextFields(unleashContext); + } else if (clientState == ClientState.ready) { _updateContextFields(unleashContext); await _fetchToggles(); } else { @@ -295,6 +301,7 @@ class UnleashClient extends EventEmitter { } Future start() async { + started = true; if (clientState == ClientState.initializing) { await _waitForEvent('initialized'); } diff --git a/test/unleash_proxy_client_flutter_test.dart b/test/unleash_proxy_client_flutter_test.dart index d5d6c56..b202ec4 100644 --- a/test/unleash_proxy_client_flutter_test.dart +++ b/test/unleash_proxy_client_flutter_test.dart @@ -550,6 +550,54 @@ void main() { ]); }); + test('update context with await', () async { + final getMock = GetMock(); + final unleash = UnleashClient( + url: url, + clientKey: 'proxy-123', + appName: 'flutter-test', + sessionIdGenerator: generateSessionId, + storageProvider: InMemoryStorageProvider(), + fetcher: getMock); + + await unleash.updateContext(UnleashContext( + userId: '123', + remoteAddress: 'address', + sessionId: 'session', + properties: {'customKey': 'customValue'})); + await unleash.start(); + + expect(getMock.calledTimes, 1); + expect(getMock.calledWithUrls, [ + Uri.parse( + 'https://app.unleash-hosted.com/demo/api/proxy?userId=123&remoteAddress=address&sessionId=session&customKey=customValue') + ]); + }); + + test('update context without await', () async { + final getMock = GetMock(); + final unleash = UnleashClient( + url: url, + clientKey: 'proxy-123', + appName: 'flutter-test', + sessionIdGenerator: generateSessionId, + storageProvider: InMemoryStorageProvider(), + fetcher: getMock); + + unleash.updateContext(UnleashContext( + userId: '123', + remoteAddress: 'address', + sessionId: 'session', + properties: {'customKey': 'customValue'})); + await unleash.start(); + + expect(getMock.calledTimes, 1); + expect(getMock.calledWithUrls, [ + Uri.parse( + 'https://app.unleash-hosted.com/demo/api/proxy?userId=123&remoteAddress=address&sessionId=session&customKey=customValue') + ]); + }); + test('set context field should wait on asynchronous start', () async { final getMock = GetMock(); final unleash = UnleashClient(