Flutter Product Experiences

Learn how to set up Remote Config for your Flutter application.

Overview

With the CleverTap Flutter SDK, you can create variables on your app that take on new values from the CleverTap dashboard. Using variables in the CleverTap dashboard allows you to roll out changes without pushing an update through the Apple App Store/Google Play Store. These can also be used in A/B tests to test features for only a percentage of your users.

You can define variables using the CleverTap Flutter SDK. When you define a variable in your code, you can sync it to the CleverTap Dashboard via the provided SDK methods.

πŸ“˜

SDK Prerequisite

Before you proceed, check that you are using Flutter SDK v1.7.0 or higher to use the Flutter Product Experiences feature.

Supported Variable Types

Currently, CleverTap SDK supports the following variable types:

  • String
  • boolean
  • Map Object
  • Number
  • int
  • float
  • double

Web platform only supports primitive data types: String, Number, and Boolean

Define Variables

Variables can be defined using the defineVariables method. You must provide the names and default values of the variables using a Map Object.

const variables = {
              'flutter_var_string': 'flutter_var_string_value',
              'flutter_var_map': {
                'flutter_var_map_string': 'flutter_var_map_value'
              },
              'flutter_var_int': 6,
              'flutter_var_float': 6.9,
              'flutter_var_boolean': true
            };
CleverTapPlugin.defineVariables(variables);

Define File Type Variables

CleverTap supports file type variables for CleverTap Flutter SDK v3.0.0 and above. Supported file types include but are not limited to images (jpg, jpeg, png, gif), text files, and PDF files.

You can define a new file type variable using the following code:

CleverTapPlugin.defineFileVariable("fileVariable");

Setup Callbacks

CleverTap Flutter SDK provides several callbacks for the developer to receive feedback from the SDK. You can use them per your requirement, using all of them is not mandatory.

Flutter SDK version lower than 3.0.0

The CleverTap Flutter SDK 3.0.0 and lower supports the following callbacks:

  • Status of fetch variables request
  • onVariablesChanged
  • onValueChanged

Status of Variables Fetch Request

This method provides a boolean flag to ensure that the variables are successfully fetched from the server.

CleverTapPlugin.setState(() async {
    bool? success = await CleverTapPlugin.fetchVariables();
    print("fetchVariables result: " + success.toString());
});

onVariablesChanged

This callback is invoked when variables are initialized with values fetched from the server. It is called each time new values are fetched.

CleverTapPlugin.onVariablesChanged((variables) {
      print("onVariablesChanged: " + variables.toString());
});

onValueChanged

This callback is invoked when the value of the variable changes. You must provide the name of the variable whose value needs to be observed.

CleverTapPlugin.onValueChanged('flutter_var_string', (variable) {
      print("onValueChanged: " + variable.toString());
});

Callbacks supported for Flutter SDK version 3.0.0 and above

The following callbacks are supported for the Flutter SDK version 3.3.0 and above:

  • onOneTimeVariablesChanged
  • onVariablesChangedAndNoDownloadsPending
  • onceVariablesChangedAndNoDownloadsPending
  • onFileValueChanged

onOneTimeVariablesChanged

This callback is invoked only once when variables are initialized or updated with server values. It is triggered on app start or when the callback is added if the values are already available.

CleverTapPlugin.onOneTimeVariablesChanged((variables) {
    print("onOneTimeVariablesChanged: " + variables.toString());
});

onVariablesChangedAndNoDownloadsPending

This callback is invoked every time variable values are updated and all associated files are downloaded and ready for use.

CleverTapPlugin.onVariablesChangedAndNoDownloadsPending((variables) => {
    print('onVariablesChangedAndNoDownloadsPending', variables);
});

onceVariablesChangedAndNoDownloadsPending

This callback is invoked only once when variable values are updated and their associated files are downloaded and ready for use. It is triggered only the first time new values are fetched and downloaded.

CleverTapPlugin.onceVariablesChangedAndNoDownloadsPending((variables) => {
    print('onceVariablesChangedAndNoDownloadsPending', variables);
});

onFileValueChanged

This callback is registered per file variable. It is called when the file associated with the file variable is downloaded and ready to be used.

CleverTapPlugin.onFileValueChanged('fileVariable', (variable) => {
    print('onFileValueChanged: ', variable);
});

Sync Defined Variables

After defining your variables in the code, you must send/sync variables to the server. To do so, the app must be in DEBUG mode and mark a particular CleverTap user profile as a test profile from the CleverTap dashboard. Learn how to mark a profile as Test Profile

After marking the profile as a test profile, you must sync the app variables in DEBUG mode:

// 1. Define CleverTap variables 
// …
// 2. Add variables/values changed callbacks
// …
// 3. Sync CleverTap Variables from DEBUG mode/builds
CleverTapPlugin.syncVariables();

πŸ“˜

Key Points to Remember

  • In a scenario where there is already a draft created by another user profile in the dashboard, the sync call will fail to avoid overriding important changes made by someone else. In this case, Publish or Dismiss the existing draft before you proceed with syncing variables again. However, you can override a draft you created via the sync method previously to optimize the integration experience.
  • You can receive the following Logcat logs from the CleverTap SDK:
    • Variables synced successfully.
    • Unauthorized access from a non-test profile. To address this, mark the profile as a test profile from the CleverTap dashboard.

Fetch Variables During a Session

You can fetch the updated values for your CleverTap variables from the server during a session. If variables have changed, the appropriate callbacks will be fired. The provided callback provides a boolean flag that indicates if the fetch call was successful. The callback is fired regardless of whether the variables have changed or not.

CleverTapPlugin.fetchVariables().then((success) => {
      print("fetchVariables result: "+ success.toString())
});

Use Fetched Variables Values

This process involves the following two major steps:

  1. Fetch variable values.
  2. Access variable values.

Fetch Variable Values

Variables are updated automatically when server values are received. If you want to receive feedback when a specific variable is updated, use the individual callback:

CleverTapPlugin.onValueChanged('flutter_var_string', (variable) {
      print("onValueChanged: " + variable.toString());
});

Access Variable Values

You can access these fetched values in the following two ways:

Getting all variables

CleverTapPlugin.getVariables().then((variables) => {
      print("getVariables: " + variables.toString())
});

Getting a specific variable

CleverTapPlugin.getVariable('flutter_var_string').then((variable) => {
      print('variable value for key \'flutter_var_string\': ' + variable.toString())
});