Demonstrates how to use the launchdarkly_flutter plugin.
In this example app, replace the string YOUR_MOBILE_KEY
with your mobile key from your Environments page.
You just need to instantiate the class and initiate the plugin with your mobile key and the user information, before checking the flags.
// Platform messages are asynchronous, so we initialize in an async method.
LaunchdarklyFlutter launchdarklyFlutter = LaunchdarklyFlutter();
try {
await launchdarklyFlutter.init('YOUR_MOBILE_KEY', 'USER_ID');
} on PlatformException {}
Be sure to use a mobile key from your Environments page. Never embed a server-side SDK key into a mobile application. Check LaunchDarkly's documentation for in-depth instructions on configuring and using LaunchDarkly.
Give some time for the initialization process to fetch new flags values (or risk getting the defaults right away), and check them:
// Platform messages are asynchronous, so we fetch flags in an async method.
bool shouldShowButton;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
shouldShowButton = await launchdarklyFlutter.boolVariation('FLAG_KEY', false);
} on PlatformException {
shouldShowButton = false;
}
Replace FLAG_KEY
with your flag key.
You can pass custom attributes using custom
argument in init
method, e.g.:
final customAttrs = {
'string': 'value',
'boolean': true,
'number': 10,
};
await launchdarklyFlutter.init(mobileKey, userId, custom: customAttrs);
Your custom attributes map should have keys of type String
and values of type String | bool | number
(for deleting an attribute remove the key or set value to null
).
If your app is used by multiple users on a single device, you may want to change users and have separate flag settings for each user.
You can use the identify method to switch user contexts:
await launchdarklyFlutter.identify(userId, custom: customAttrs);