Skip to main content

Dart (Flutter) SDK Reference

pub package Dart CI

ConfigCat Dart (Flutter) SDK on GitHub

Getting Started​

1. Add the ConfigCat SDK to your project​

With Dart:

dart pub add configcat_client

With Flutter:

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: ^1.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. Get your setting value​

final isMyAwesomeFeatureEnabled = await client.getValue(key: '<key-of-my-awesome-feature>', defaultValue: false);
if(isMyAwesomeFeatureEnabled) {
doTheNewThing();
} else {
doTheOldThing();
}

5. Close ConfigCat client​​

You can safely shut down all clients at once or individually and release all associated resources on application exit.

ConfigCatClient.close(); // closes all clients

ConfigCatClient.close(client: client); // closes a specific client

Configuring 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.

PropertiesDescription
dataGovernanceOptional, 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.
baseUrlOptional, sets the CDN base url (forward proxy, dedicated subscription) from where the sdk will download the configurations.
connectTimeoutOptional, sets the underlying Dio HTTP client's connect timeout. More about the HTTP Client.
receiveTimeoutOptional, sets the underlying Dio HTTP client's receive timeout. More about the HTTP Client.
sendTimeoutOptional, sets the underlying Dio HTTP client's send timeout. More about the HTTP Client.
cacheOptional, sets a custom cache implementation for the client. More about cache.
modeOptional, sets the polling mode for the client. More about polling modes.
loggerOptional, sets the internal logger and log level. More about logging.
overrideOptional, configures local feature flag & setting overrides. More about feature flag overrides.
final client = ConfigCatClient.get(
sdkKey: '#YOUR-SDK-KEY#',
options: ConfigCatOptions(
mode: PollingMode.manualPoll(),
logger: ConfigCatLogger(level: LogLevel.info)
)
);
caution

We strongly recommend you to use the ConfigCatClient as a Singleton object in your application. The ConfigCatClient constructs singleton client instances for your SDK keys with its ConfigCatClient.get(sdkKey: <sdkKey>) static factory method. These clients can be closed all at once or individually with the ConfigCatClient.close() method.

Anatomy of getValue()​

ParametersDescription
keyREQUIRED. Setting-specific key. Set on ConfigCat Dashboard for each setting.
defaultValueREQUIRED. This value will be returned in case of an error.
userOptional, 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
);

User Object​

The User Object is essential if you'd like to use ConfigCat's Targeting feature.

final user = ConfigCatUser(identifier: '435170f4-8a8b-4b67-a723-505ac7cdea92');   
final user = ConfigCatUser(identifier: '[email protected]');   

Customized user object creation:​

ArgumentDescription
identifierREQUIRED. Unique identifier of a user in your application. Can be any value, even an email address.
emailOptional parameter for easier targeting rule definitions.
countryOptional parameter for easier targeting rule definitions.
customOptional dictionary for custom attributes of a user for advanced targeting rule definitions. e.g. User role, Subscription type.
final user = ConfigCatUser(
identifier: '435170f4-8a8b-4b67-a723-505ac7cdea92',
email: '[email protected]',
country: 'United Kingdom',
custom: {
'SubscriptionType': 'Pro',
'UserRole': 'Admin'
}
);

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();

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.

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(
mode: PollingMode.autoPoll(
autoPollInterval: Duration(seconds: 100),
),
)
);

Adding a callback to configurationChangeListener option parameter will get you notified about changes.

final client = ConfigCatClient.get(
sdkKey: '<PLACE-YOUR-SDK-KEY-HERE>',
options: ConfigCatOptions(
mode: PollingMode.autoPoll(
autoPollInterval: Duration(seconds: 100),
onConfigChanged: () {
// here you can subscribe to configuration changes
}
),
)
);

Available options:

Option ParameterDescriptionDefault
autoPollIntervalPolling interval.60
maxInitWaitTimeMaximum waiting time between the client initialization and the first config acquisition in seconds.5
onConfigChangedCallback to get notified about changes.-

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 the 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(
mode: PollingMode.lazyLoad(
// the cache will expire in 100 seconds
cacheRefreshInterval: Duration(seconds: 100),
),
)
);

Available options:

ParameterDescriptionDefault
cacheRefreshIntervalCache 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(
mode: PollingMode.manualPoll(),
)
);

client.forceRefresh();

getValue() returns defaultValue if the cache is empty. Call forceRefresh() to update the cache.

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/Offline mode (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
)
)
);

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()));

HttpClient​

The ConfigCat SDK internally uses a Dio HTTP client instance to download the latest configuration 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 seconds
  • sendTimeout: 20 seconds
  • receiveTimeout: 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​

Any time you want to refresh the cached configuration with the latest one, you can call the forceRefresh() method of the library, which initiates a new download and updates the local cache.

Logging​

The default logger used by the SDK is the logger package, but you can override it with your implementation via the logger client option. The custom implementation must satisfy the Logger abstract class.

final client = ConfigCatClient.get(
sdkKey: '<PLACE-YOUR-SDK-KEY-HERE>',
options: ConfigCatOptions(
logger: ConfigCatLogger(internalLogger: MyCustomLogger()),
)
);

You can customize the internally used logger instance.

import 'package:logger/logger.dart' as extlog;

final client = ConfigCatClient.get(
sdkKey: '<PLACE-YOUR-SDK-KEY-HERE>',
options: ConfigCatOptions(
logger: ConfigCatLogger(
internalLogger: DefaultLogger(internalLogger: extlog.Logger(/* customization of the logger package */))
),
)
);

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:

LevelDescription
nothingTurn the logging off.
errorOnly error level events are logged.
warningDefault. Errors and Warnings are logged.
infoErrors, Warnings and feature flag evaluation is logged.
debugAll 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:

[I] TIME: 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

Look Under the Hood​