Skip to content

Expose ld_device ID via a Dart API method #206

@Azzeccagarbugli

Description

@Azzeccagarbugli

Currently, the LaunchDarkly Flutter SDK stores a device-level unique identifier (the ld_device context) using SharedPreferences, but this value is not accessible through the public API. I have resorted to manually reading shared preferences to retrieve it, which is brittle and relies on internal key formats that may change.

💡 Motivation

  • The device ID is essential for correlating evaluations or analytics across devices and backend systems.
  • There’s currently no safe, stable alternative to manually parsing shared preferences, which risks breaking in future versions.
  • Offering official access would improve developer experience and support valid use cases like debugging, analytics, or correlating logs.

❌ Actual implementation

/// Encoder method
String _encodePersistenceKey(String input) {
  final bytes = utf8.encode(input);
  final digest = sha256.convert(bytes);
  // This will be the hex encoded digest.
  return digest.toString();
}

final _preferences = await SharedPreferences.getInstance();

final _deviceIdLaunchy = _preferences.getString(
  'LaunchDarkly_AutoEnvContextKey.${_encodePersistenceKey('ld_device')}',
);

debugPrint('deviceId: $_deviceIdLaunchy');

✅ Desired API

Expose a new asynchronous method on LDClient, such as:

Future<String?> getDeviceId();

That returns the same ld_device UUID stored internally. It could be available once the client has been started.

🛠️ Proposed Implementation

  1. Dart-side: add getDeviceId() to LDClient in launchdarkly_flutter_client_sdk.dart, exposing the value.
  2. Testing: Add unit tests that confirm the device ID matches across calls and persists across restarts.
  3. Documentation: Include usage in README and CHANGELOG.

I’d be happy to draft a PR implementing this feature, including all necessary code, tests, and documentation.

Please let me know if this aligns with the project’s goals, I can begin right away 🙏

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions