Skip to Content
SDKJavaEvaluate Flags

getFlag(key, context)

Returns the data for a specific flag, or null if the flag does not exist.

var flag = client.getFlag("new-checkout", new FlagEvaluationContext("user-42")); // FlagData or null

The user id in the context is used to evaluate rollout flags consistently: the same user always gets the same result. When the context or user id is empty, a per-instance anonymous ID is used.

Safe check

if (flag != null && flag.isEnabled()) { // Flag is enabled for this user }

getFlags(context)

Returns all flags for the environment as a list.

var flags = client.getFlags(new FlagEvaluationContext("user-42")); for (var flag : flags) { System.out.println(flag.getKey() + ": " + flag.isEnabled()); }

Examples by flag type

Boolean flag

var maintenance = client.getFlag("maintenance-mode", context); if (maintenance != null && maintenance.isEnabled()) { return maintenancePage(); }

Rollout flag

var newDashboard = client.getFlag("new-dashboard", context); // flag.isEnabled() is true or false based on userId and percentage if (newDashboard != null && newDashboard.isEnabled()) { return newDashboardPage(); } // To see the configured percentage: if (newDashboard != null && "rollout".equals(newDashboard.getType())) { System.out.println(newDashboard.getPercent() + "% of users will see this feature"); }

Before init()

If you call getFlag() before init(), the flag will return null (the local cache is still empty).

var client = new CanaryGateClient("cg_key"); // Do not do this: client.getFlag("feature"); // null // Do this instead: client.init(); client.getFlag("feature"); // FlagData or null

Flags that do not exist

If the flag does not exist in the environment, getFlag() returns null. Always guard with a null check to avoid breaking the application flow:

// If 'new-feature' does not exist in the environment, enabled will be false var flag = client.getFlag("new-feature", context); boolean enabled = flag != null && flag.isEnabled();
Last updated on