Evaluating flags in non-UI code
The OpenFeatureClient
class can be used to evaluate flags in non-UI code.
Let's head to counter_page.dart
where we have incrementCounter
method that is responsible
for incrementing the counter. Here, we'll make it so that the increment factor is decided based on
the email of user.
Update the method as given below:
Future<void> _incrementCounter() async {
final client = OpenFeature.instance.getClient();
final increment = await client.getNumberValue(
key: 'increment',
defaultValue: 1,
context: EvaluationContext({'email': widget.email ?? ''}),
);
_counter += increment as int;
setState(() {});
}
The evaluation process is similar to that of previous step. For instance, if you login with email
user@feature.flag
then the flag will be resolved with "double" variant. Thus, will increment the
counter by 2 in each tap of the FAB.