Dart (Flutter) SDK Reference
ConfigCat Dart (Flutter) SDK on GitHub
This documentation applies to the v3.x version of the ConfigCat Dart (Flutter). For the documentation of the latest release, please refer to this page.
Getting Started
1. Add the ConfigCat SDK to your project
- Dart
- Flutter
dart pub add configcat_client
flutter pub add configcat_client
Or put the following directly to your pubspec.yml
and run dart pub get
or flutter pub get
.
dependencies:
configcat_client: ^3.0.0
2. Import the ConfigCat SDK
import 'package:configcat_client/configcat_client.dart';
3. Create the ConfigCat client with your SDK Key
final client = ConfigCatClient.get(sdkKey: '#YOUR-SDK-KEY#');
4. (Optional) Set up Flutter caching
If you're using the SDK in a Flutter application, it's recommended to use the Flutter Preferences Cache implementation for caching. It stores the downloaded config JSON
using the shared_preferences package.
import 'package:configcat_preferences_cache/configcat_preferences_cache.dart';
final client = ConfigCatClient.get(
sdkKey: '#YOUR-SDK-KEY#',
options: ConfigCatOptions(cache: ConfigCatPreferencesCache()));
5. Get your setting value
final isMyAwesomeFeatureEnabled = await client.getValue(key: 'isMyAwesomeFeatureEnabled', defaultValue: false);
if (isMyAwesomeFeatureEnabled) {
doTheNewThing();
} else {
doTheOldThing();
}
6. Close ConfigCat client
You can safely shut down all clients at once or individually and release all associated resources on application exit.
ConfigCatClient.closeAll(); // closes all clients
client.close(); // closes the specific client
Setting up the ConfigCat Client
ConfigCat Client is responsible for:
- managing the communication between your application and ConfigCat servers.
- caching your setting values and feature flags.
- serving values quickly in a failsafe way.
ConfigCatClient.get(sdkKey: <sdkKey>)
returns a client with default options.
Customizing the ConfigCat Client
To customize the SDK's behavior, you can pass an additional ConfigCatOptions
parameter to the get()
static
factory method where the ConfigCatOptions
class is used to set up the ConfigCat Client.
final client = ConfigCatClient.get(
sdkKey: '#YOUR-SDK-KEY#',
options: ConfigCatOptions(
pollingMode: PollingMode.manualPoll(),
logger: ConfigCatLogger(level: LogLevel.info)
)
);
These are the available options on the ConfigCatOptions
class:
Properties | Description |
---|---|
dataGovernance | Optional, defaults to global . Describes the location of your feature flag and setting data within the ConfigCat CDN. This parameter needs to be in sync with your Data Governance preferences. More about Data Governance. Available options: global , euOnly . |
baseUrl | Optional, sets the CDN base url (forward proxy, dedicated subscription) from where the sdk will download the config JSON. |
connectTimeout | Optional, sets the underlying Dio HTTP client's connect timeout. More about the HTTP Client. |
receiveTimeout | Optional, sets the underlying Dio HTTP client's receive timeout. More about the HTTP Client. |
sendTimeout | Optional, sets the underlying Dio HTTP client's send timeout. More about the HTTP Client. |
cache | Optional, sets a custom cache implementation for the client. More about cache. |
pollingMode | Optional, sets the polling mode for the client. More about polling modes. |
logger | Optional, sets the internal logger and log level. More about logging. |
override | Optional, sets local feature flag & setting overrides. More about feature flag overrides. |
defaultUser | Optional, sets the default user. More about default user. |
offline | Optional, defaults to false . Indicates whether the SDK should be initialized in offline mode. More about offline mode. |
hooks | Optional, used to subscribe events that the SDK sends in specific scenarios. More about hooks. |
We strongly recommend you to use the ConfigCatClient
as a Singleton object in your application.
The ConfigCatClient.get(sdkKey: <sdkKey>)
static factory method constructs singleton client instances for your SDK keys.
These clients can be closed all at once with the ConfigCatClient.closeAll()
method or individually with client.close()
.
Anatomy of getValue()
Parameters | Description |
---|---|
key | REQUIRED. The key of a specific setting or feature flag. Set on ConfigCat Dashboard for each setting. |
defaultValue | REQUIRED. This value will be returned in case of an error. |
user | Optional, User Object. Essential when using Targeting. Read more about Targeting. |
final value = await client.getValue(
key: 'keyOfMySetting',
defaultValue: false,
user: ConfigCatUser(identifier: '#USER-IDENTIFIER#'), // Optional User Object
);
Anatomy of getValueDetails()
getValueDetails()
is similar to getValue()
but instead of returning the evaluated value only, it gives more detailed information about the evaluation result.
Parameters | Description |
---|---|
key | REQUIRED. The key of a specific setting or feature flag. Set on ConfigCat Dashboard for each setting. |
defaultValue | REQUIRED. This value will be returned in case of an error. |
user | Optional, User Object. Essential when using Targeting. Read more about Targeting. |
final details = await client.getValueDetails(
key: 'keyOfMySetting',
defaultValue: false,
user: ConfigCatUser(identifier: '#USER-IDENTIFIER#'), // Optional User Object
);
The details
result contains the following information:
Field | Type | Description |
---|---|---|
value | Bool / String / Int / Double | The evaluated value of the feature flag or setting. |
key | String | The key of the evaluated feature flag or setting. |
isDefaultValue | Bool | True when the default value passed to getValueDetails() is returned due to an error. |
error | String? | In case of an error, this field contains the error message. |
user | ConfigCatUser? | The User Object that was used for evaluation. |
matchedEvaluationPercentageRule | PercentageRule? | If the evaluation was based on a percentage rule, this field contains that specific rule. |
matchedEvaluationRule | RolloutRule? | If the evaluation was based on a Targeting Rule, this field contains that specific rule. |
fetchTime | DateTime | The last download time of the current config. |
User Object
The User Object is essential if you'd like to use ConfigCat's Targeting feature.
final user = ConfigCatUser(identifier: '#UNIQUE-USER-IDENTIFIER#');
final user = ConfigCatUser(identifier: '[email protected]');
Customized User Object creation
Argument | Description |
---|---|
identifier | REQUIRED. Unique identifier of a user in your application. Can be any value, even an email address. |
email | Optional parameter for easier Targeting Rule definitions. |
country | Optional parameter for easier Targeting Rule definitions. |
custom | Optional dictionary for custom attributes of a user for advanced Targeting Rule definitions. e.g. User role, Subscription type. |
final user = ConfigCatUser(
identifier: '#UNIQUE-USER-IDENTIFIER#',
email: '[email protected]',
country: 'United Kingdom',
custom: {
'SubscriptionType': 'Pro',
'UserRole': 'Admin'
}
);
Default user
There's an option to set a default User Object that will be used at feature flag and setting evaluation. It can be useful when your application has a single user only, or rarely switches users.
You can set the default User Object either on SDK initialization:
final client = ConfigCatClient.get(
sdkKey: '#YOUR-SDK-KEY#',
options: ConfigCatOptions(
defaultUser: ConfigCatUser(identifier: '[email protected]')
)
);
or with the setDefaultUser()
method of the ConfigCat client.
client.setDefaultUser(ConfigCatUser(identifier: '[email protected]'));
Whenever the getValue()
, getValueDetails()
, getAllValues()
, or getAllVariationIds()
methods are called without an explicit user
parameter, the SDK will automatically use the default user as a User Object.
final user = ConfigCatUser(identifier: '[email protected]');
client.setDefaultUser(user);
// The default user will be used at the evaluation process.
final value = await client.getValue(key: 'keyOfMySetting', defaultValue: false);
When the user
parameter is specified on the requesting method, it takes precedence over the default user.
final user = ConfigCatUser(identifier: '[email protected]');
client.setDefaultUser(user);
final otherUser = ConfigCatUser(identifier: '[email protected]');
// otherUser will be used at the evaluation process.
final value = await client.getValue(key: 'keyOfMySetting', defaultValue: false, user: otherUser);
For deleting the default user, you can do the following:
client.clearDefaultUser();
Polling Modes
The ConfigCat SDK supports 3 different polling mechanisms to acquire the setting values from ConfigCat. After latest setting values are downloaded, they are stored in the internal cache, then all getValue()
calls are served from there. With the following polling modes, you can customize the SDK to best fit to your application's lifecycle.
More about polling modes.
Auto polling (default)
The ConfigCat SDK downloads the latest values and stores them automatically every 60 seconds.
Use the the autoPollInterval
option parameter of the PollingMode.autoPoll()
to change the polling interval.
final client = ConfigCatClient.get(
sdkKey: '<PLACE-YOUR-SDK-KEY-HERE>',
options: ConfigCatOptions(
pollingMode: PollingMode.autoPoll(
autoPollInterval: Duration(seconds: 100),
),
)
);
Available options:
Option Parameter | Description | Default |
---|---|---|
autoPollInterval | Polling interval. | 60 |
maxInitWaitTime | Maximum waiting time between the client initialization and the first config acquisition in seconds. | 5 |
Lazy Loading
When calling getValue()
, the ConfigCat SDK downloads the latest setting values if they are not present or expired in the cache. In this case getValue()
will return the setting value after the cache is updated.
Use the cacheRefreshInterval
option parameter of the PollingMode.lazyLoad()
to set cache lifetime.
final client = ConfigCatClient.get(
sdkKey: '<PLACE-YOUR-SDK-KEY-HERE>',
options: ConfigCatOptions(
pollingMode: PollingMode.lazyLoad(
// the cache will expire in 100 seconds
cacheRefreshInterval: Duration(seconds: 100),
),
)
);
Available options:
Parameter | Description | Default |
---|---|---|
cacheRefreshInterval | Cache TTL. | 60 |
Manual Polling
Manual polling gives you full control over when the config JSON
(with the setting values) is downloaded. ConfigCat SDK will not update them automatically. Calling forceRefresh()
is your application's responsibility.
final client = ConfigCatClient.get(
sdkKey: '<PLACE-YOUR-SDK-KEY-HERE>',
options: ConfigCatOptions(
pollingMode: PollingMode.manualPoll(),
)
);
client.forceRefresh();
getValue()
returnsdefaultValue
if the cache is empty. CallforceRefresh()
to update the cache.
Hooks
With the following hooks you can subscribe to particular events fired by the SDK:
-
onClientReady()
: This event is sent when the SDK reaches the ready state. If the SDK is initialized with lazy load or manual polling, it's considered ready right after instantiation. If it's using auto polling, the ready state is reached when the SDK has a valid config JSON loaded into memory either from cache or from HTTP. If the config couldn't be loaded neither from cache nor from HTTP theonClientReady
event fires when the auto polling'smaxInitWaitTime
is reached. -
onConfigChanged(Map<string, Setting>)
: This event is sent when the SDK loads a valid config JSON into memory from cache, and each subsequent time when the loaded config JSON changes via HTTP. -
onFlagEvaluated(EvaluationDetails)
: This event is sent each time when the SDK evaluates a feature flag or setting. The event sends the same evaluation details that you would get fromgetValueDetails()
. -
onError(String)
: This event is sent when an error occurs within the ConfigCat SDK.
You can subscribe to these events either on SDK initialization:
final client = ConfigCatClient.get(
sdkKey: '#YOUR-SDK-KEY#',
options: ConfigCatOptions(
pollingMode: PollingMode.manualPoll(),
hooks: Hooks(
onFlagEvaluated: (details) => /* handle the event */
)
)
);
or with the hooks
property of the ConfigCat client:
client.hooks.addOnFlagEvaluated((details) => /* handle the event */);
Online / Offline mode
In cases when you'd want to prevent the SDK from making HTTP calls, you can put it in offline mode:
client.setOffline();
In offline mode, the SDK won't initiate HTTP requests and will work only from its cache.
To put the SDK back in online mode, you can do the following:
client.setOnline();
With
client.isOffline()
you can check whether the SDK is in offline mode.
Flag Overrides
With flag overrides you can overwrite the feature flags & settings downloaded from the ConfigCat CDN with local values. Moreover, you can specify how the overrides should apply over the downloaded values. The following 3 behaviours are supported:
-
Local only (
OverrideBehaviour.localOnly
): When evaluating values, the SDK will not use feature flags & settings from the ConfigCat CDN, but it will use all feature flags & settings that are loaded from local-override sources. -
Local over remote (
OverrideBehaviour.localOverRemote
): When evaluating values, the SDK will use all feature flags & settings that are downloaded from the ConfigCat CDN, plus all feature flags & settings that are loaded from local-override sources. If a feature flag or a setting is defined both in the downloaded and the local-override source then the local-override version will take precedence. -
Remote over local (
OverrideBehaviour.remoteOverLocal
): When evaluating values, the SDK will use all feature flags & settings that are downloaded from the ConfigCat CDN, plus all feature flags & settings that are loaded from local-override sources. If a feature flag or a setting is defined both in the downloaded and the local-override source then the downloaded version will take precedence.
You can set up the SDK to load your feature flag & setting overrides from a Map<String, Object>
.
final client = ConfigCatClient.get(
sdkKey: 'localhost',
options: ConfigCatOptions(
override: FlagOverrides(
dataSource: OverrideDataSource.map({
'enabledFeature': true,
'disabledFeature': false,
'intSetting': 5,
'doubleSetting': 3.14,
'stringSetting': 'test',
}),
behaviour: OverrideBehaviour.localOnly
)
)
);
getAllKeys()
You can query the keys of each feature flag and setting with the getAllKeys()
method.
final client = ConfigCatClient.get(sdkKey: '#YOUR-SDK-KEY#');
final keys = await client.getAllKeys();
getAllValues()
Evaluates and returns the values of all feature flags and settings. Passing a User Object is optional.
final client = ConfigCatClient.get(sdkKey: '#YOUR-SDK-KEY#');
final settingValues = await client.getAllValues();
// invoke with User Object
final user = ConfigCatUser(identifier: '#UNIQUE-USER-IDENTIFIER#');
final settingValuesTargeting = await client.getAllValues(user);
Cache
The SDK caches the downloaded config JSON
only in memory by default. In case you have a Flutter application, you can use the Flutter Preferences Cache for caching.
It's based on the shared_preferences package that uses the following storage locations by platform:
- Web: Browser
LocalStorage
. - iOS / macOS:
NSUserDefaults
. - Android:
SharedPreferences
. - Linux: File in
XDG_DATA_HOME
directory. - Windows: File in roaming
AppData
directory.
final client = ConfigCatClient.get(
sdkKey: '#YOUR-SDK-KEY#',
options: ConfigCatOptions(cache: ConfigCatPreferencesCache()));
Custom Cache
You have the option to inject your custom cache implementation into the client. All you have to do is to inherit from the ConfigCatCache
abstract class:
class MyCustomCache extends ConfigCatCache {
Future<String> read(String key) {
// here you have to return with the cached value
}
Future<void> write(String key, String value) {
// here you have to store the new value in the cache
}
}
Then use your custom cache implementation:
final client = ConfigCatClient.get(
sdkKey: '<PLACE-YOUR-SDK-KEY-HERE>',
options: ConfigCatOptions(cache: MyCustomCache()));
The Dart (Flutter) SDK supports shared caching. You can read more about this feature and the required minimum SDK versions here.
HttpClient
The ConfigCat SDK internally uses a Dio HTTP client instance to download the latest config JSON over HTTP. You have the option to customize the internal HTTP client.
HTTP Timeout
You can set the maximum wait time for a ConfigCat HTTP response by using Dio's timeouts.
final client = ConfigCatClient.get(
sdkKey: '<PLACE-YOUR-SDK-KEY-HERE>',
options: ConfigCatOptions(
connectTimeout: Duration(seconds: 10), // timeout for establishing a HTTP connection with the server
sendTimeout: Duration(seconds: 10), // timeout for sending a HTTP request to the server
receiveTimeout: Duration(seconds: 10), // timeout for reading the server's HTTP response
)
);
Default timeout values:
connectTimeout
: 10 secondssendTimeout
: 20 secondsreceiveTimeout
: 20 seconds
HTTP Proxy
If your application runs behind a proxy you can do the following:
import 'package:dio/adapter.dart';
(client.httpClient.httpClientAdapter as DefaultHttpClientAdapter)
.onHttpClientCreate = (client) {
client.findProxy = (uri) {
return 'PROXY proxyHost:proxyPort';
};
};
Force refresh
Call the forceRefresh()
method on the client to download the latest config JSON and update the cache.
Logging
The default logger used by the SDK simply outputs the log messages with print()
, but you can override it with your implementation via the logger
client option. The custom implementation must satisfy the Logger abstract class.
class MyCustomLogger implements Logger {
void close() {
// close the logger
}
void debug(message, [error, StackTrace? stackTrace]) {
// write the debug logs
}
void error(message, [error, StackTrace? stackTrace]) {
// write the error logs
}
void info(message, [error, StackTrace? stackTrace]) {
// write the info logs
}
void warning(message, [error, StackTrace? stackTrace]) {
// write the warning logs
}
}
Then you can use your custom logger implementation at the SDK's initialization:
final client = ConfigCatClient.get(
sdkKey: '<PLACE-YOUR-SDK-KEY-HERE>',
options: ConfigCatOptions(
logger: ConfigCatLogger(internalLogger: MyCustomLogger()),
)
);
You can change the verbosity of the logs by passing a LogLevel
parameter to the logger
option.
final client = ConfigCatClient.get(
sdkKey: '<PLACE-YOUR-SDK-KEY-HERE>',
options: ConfigCatOptions(
logger: ConfigCatLogger(level: LogLevel.info),
)
);
Available log levels:
Level | Description |
---|---|
nothing | Turn the logging off. |
error | Only error level events are logged. |
warning | Default. Errors and Warnings are logged. |
info | Errors, Warnings and feature flag evaluation is logged. |
debug | All of the above plus debug info is logged. Debug logs can be different for other SDKs. |
Info level logging helps to inspect how a feature flag was evaluated:
[INFO] 2022-01-20T18:22:02.313703 ConfigCat - Evaluating getValue(isPOCFeatureEnabled)
User object: {Identifier: 435170f4-8a8b-4b67-a723-505ac7cdea92, Email: [email protected]}
Evaluating rule: [Email:[email protected]] [CONTAINS] [@something.com] => no match
Evaluating rule: [Email:[email protected]] [CONTAINS] [@example.com] => match, returning: true
Sample Apps
Check out our Sample Applications how they use the ConfigCat SDK
Guides
See this guide on how to use ConfigCat's Dart SDK.